DUBAI, July 18 (Reuters) - When intelligence officials briefed Iran's supreme leader Ayatollah Ali Khamenei in May ahead of a snap presidential election, their report was grim: angered by economic hardship and crackdowns on social freedoms, most Iranians planned to boycott the vote and turnout would only be about 13%.

That's when Khamenei decided to plan a carefully orchestrated election, setting the stage for a little-known but trusted moderate, Massoud Pezeshkian, to rise to the presidency in a race that would initially be dominated by hardliners, five people with knowledge of the matter told Reuters.

Khamenei gathered a handful of his most trusted advisers to discuss his plan in at least three meetings in late May at his residence in a fortified compound in Tehran, according to the five people, who are two hardline sources, a top security official and two insiders close to Khamenei's inner circle.

The supreme leader was concerned low turnout would damage the clerical establishment's credibility and he ordered those present to find a way to steer the election, said one of the people, who was briefed about the meetings.

The election was called after President Ebrahim Raisi died in a helicopter crash in May. His death upset the plans of many fellow hardliners who wanted him to succeed the 85-year-old Khamenei and triggered a race among hardliners to influence the selection of the next supreme leader.

The meetings at Khamenei's residence included a small group of senior officials and security aides, his close ally and adviser Ali Akbar Velayati, as well as two senior commanders of the powerful elite Revolutionary Guards.

Khamenei's aim was to preserve the Islamic Republic amid domestic dissent and heightened tensions with the West and Israel over Gaza, exacerbated by the involvement of Tehran's allies Hezbollah in Lebanon and the Houthis in Yemen, according to the five people, who were briefed in detail about what Khamenei said during the meetings regarding his plan and its goals.

One of the insiders briefed about the meeting said Khamenei believed Iran needed a president who could appeal to different layers of society, but would not challenge the ruling Shi'ite theocracy.

Several names were floated at the second meeting. Khamenei suggested Pezeshkian as a person who could foster unity among those in power, bridge the gap between the clerical establishment and the people, and ensure a smooth selection process for the next supreme leader, two sources said.

"It was a flawless plan by the supreme leader ... which guaranteed the survival of the Islamic Republic," said Tehran-based pro-reform analyst Saeed Laylaz.

"Pezeshkian will avoid any crisis at home, whether with the nation or the establishment," Laylaz said. "That will allow top leaders to decide about the succession and plan it in a calm atmosphere."

Khamenei's office, the public relations office for the Islamic Revolutionary Guard Corps and Pezeshkian's office could not be reached for comment. Velayati's office declined to comment.

ELECTION ENGINEERING?

The new president is not expected to usher in any major shift on Iran's nuclear or foreign policy, or its support for militias in the region, but he will be closely involved in selecting the successor to Khamenei, who calls the shots on top state matters.

Pezeshkian's mild profile, the sources said, would appease disgruntled Iranians, ensure domestic stability amid mounting foreign pressure, as well as providing Khamenei with a trusted ally in the eventual succession process.

A regional source close to Iranian circles of power said Pezeshkian's election had been "engineered" to defuse tensions after a wave of popular protests sparked by the death in custody of a young woman in 2022 and stricter curbs on social freedoms imposed by Raisi.

The initial phase of Khamenei's plan was set in motion when then-lawmaker Pezeshkian - encouraged by pragmatic former officials with links to the supreme leader's office - registered to stand in the June 28 election, two sources said.

They said Pezeshkian was unaware of the behind-the-scenes decisions. One source close to him said he didn't even expect to be approved by the Guardian Council, an unelected vetting body of six clerics and six jurists aligned to Khamenei which has banned many moderate and prominent conservative candidates in the past.

Khamenei's plan was designed to appear fair and democratic, so two prominent hardline candidates, former nuclear negotiator Saeed Jalili and parliament speaker Mohammad Baqer Qalibaf, were approved by the vetting council, the five people familiar with the matter said.

That meant hardliner votes would likely be split between them, making it harder for both to make it to a run-off.

Jalili belongs to the ultra-hardline camp of "Paydari," which advocates tougher social restrictions, self-reliance, a hawkish foreign policy - and is believed to have already chosen its candidate to succeed Khamenei, said former Iranian lawmaker Noureddin Pirmoazen, a reformer now based in the United States.

A win for Jalili, who opposed the 2015 nuclear deal with world powers, would have sent a negative signal to the West as it piles pressure on Tehran over its fast-advancing uranium enrichment program, three analysts and two diplomats told Reuters.

"With the increased likelihood of Donald Trump's return to the White House ... the Islamic Republic needed a moderate figure to keep dialog with the West open and reduce tensions," said one Western diplomat in the region.

A Guardian Council spokesman said: "It was a transparent and impartial election."

Jalili and Qalibaf could not be reached for comment.

A U.S. State Department spokesperson said: "We can't speculate on specific theories of what may have transpired behind the scenes of Iran's recent presidential election. What we can say with certainty is that elections in Iran are neither free nor fair."

A White House National Security Council spokesperson did not respond directly to questions about the main points of this story but said Washington had no expectation the elections would lead to fundamental change in Iran's direction or more respect for the human rights of its citizens.

THE DESIRED OUTCOME

Pezeshkian, who is an Azeri ethnic minority, won the first round with a core of voters that analysts said was mostly urban middle class or young - groups widely disillusioned by years of security crackdowns.

But voter turnout was just 40%, the lowest for any election in the Islamic Republic, and the election went to a run-off between Pezeshkian and the fervently anti-Western Jalili.

Qalibaf, a security hawk, who has echoed the views of Khamenei on every major issue, such as backing the power of Islamic clerics, finished third.

Fearing Jalili's antagonistic domestic and foreign policy, many Iranians who voted for Qalibaf, or abstained, went for Pezeshkian in the second round on July 5, bumping up the turnover to almost 50% of Iran's 61 million voters.

Ultimately, Khamenei's plan achieved the desired outcome.

Pezeshkian, a 69-year-old heart surgeon, backed by reformists, moderate conservatives and ethnic minorities, won with 54% of the votes.

"I thank the supreme leader. If it weren't for him, I don't think my name would have easily come out of ballot boxes," Pezeshkian said on state TV.

Two sources close to Khamenei said Pezeshkian was referring to an order from the supreme leader to electoral officials to ensure votes were counted properly. The electoral authorities said there were no complaints about vote rigging.

Pezeshkian, loyal to Iran's theocratic rule, has pledged to pursue a pragmatic foreign policy, ease tensions over now-stalled talks to revive a 2015 nuclear deal with major powers, and improve prospects for social liberalization.

He has spoken up for the rights of women and ethnic minorities and criticized the establishment's handling of the death of Mahsa Amini, an Iranian Kurdish woman who died in 2022 while in custody for allegedly violating the Islamic dress code.

"They arrest a girl because a few strands of her hair are showing ... and return her dead body to her family," Pezeshkian said in 2022. "This behavior is unacceptable."

However, many analysts are skeptical about whether Pezeshkian can fulfill all his campaign promises as he has publicly stated that he has no intention of confronting Iran's powerful clerics and security hawks.

(Writing by Parisa Hafezi; Editing by Samia Nakhoul and David Clarke)