When tens of thousands of Iranians took to the streets to celebrate Hassan Rohani's presidential victory last weekend, one chant stood out: "Dictator! Thank you!"
In the back of their minds, the protesters must have been asking themselves the same thing surprised Iran observers are wondering: Why would Supreme Leader Ayatollah Ali Khamenei, the "dictator" who has final say on all things in Iran, allow a relative moderate with ties to an archrival to win the presidential election?
And why, in the first election since allegations of fraud tainted the contentious 2009 vote, would the supreme leader apparently let voters determine the outcome?
The sense of disbelief over the win by Rohani, who has close ties to Khamenei rival and former President Akbar Hashemi Rafsanjani, appeared to be widespread among celebrants.
"I'm happy they counted my vote," read one handwritten poster held by a young man in Tehran whose picture was shared on social media.
In the run-up to the June 14 election, the general consensus among Iran observers was that the Iranian establishment would oppose Rohani and find a way to elevate one of the handful of conservative candidates to the presidency.
In hindsight, analysts say a number of factors factored into Rohani's surprising win. Among them were the large turnout, previously undecided voters settling on Rohani, the reemergence of the opposition movement, and the establishment's fear of a repeat of the mass street protests that erupted after the 2009 poll.
Supreme leader Ayatollah Ali Khamenei (left) meets with President-elect Hassan Rohani in Tehran on June 16.
A major factor, analysts suggest, was the conservatives' failure to unite behind a single candidate, which would have prevented their votes from being split among five contestants.
And political realities played a role in Rohani's win, including crippling sanctions on the Islamic republic that some believe effectively turned the election into a referendum on Khamenei's hard-line nuclear policies.
Said Jalili, Iran's top nuclear negotiator and the most hard-line candidate in the race, found himself on the losing end of that referendum. Many Iranians said they voted against Jalili, who parroted some of Khamenei's views in his campaign and was believed to be the supreme leader's preferred choice.
Rohani, on the other hand, promised to improve the economy while calling for moderate policies both at home and abroad. He called for an end to Iran's international isolation and linked the importance of Iran's nuclear program to people's everyday lives.
The 64-year-old Rohani is not a reformist. He is a regime insider who has held top posts in the Islamic republic. Unlike Rafsanjani, who in 2009 expressed support for the Green Opposition movement, Rohani condemned the protests as a move by "some who had been fooled." But the endorsement of Rohani by the reformist faction gave him a significant boost among young voters and those hungry for change.
Alireza Nader, an Iran expert at the Rand Corporation, says Khamenei was apparently willing to accept Rohani's win because of his profile.
"After all, he is Khamenei's representative to the Supreme National Security Council," Nader says. "In addition, he was qualified by the Guardians Council, which is very telling. Rafsanjani (whose candidacy was denied) may have been too powerful for Khamenei, but Rohani may be more pliant."
Nader says Rohani can serve as a bridge between the establishment and the reformists and Green Movement.
"The regime is trying to heal the internal divisions within Iran and alleviate external pressure," Nader says. "Rohani has a chance to do both, without seeking a wide-ranging transformation of the Islamic republic."
Eskandar Sadeghi Boroujerdi, a scholar and researcher of modern Iran at the University of Oxford's Queen's College, suggests that Khamenei came to the conclusion that it would have been too costly to deny Rohani victory.
"I think, obviously, [Khamenei's office] has [opinion polls]," Boroujerdi says, "and so they have feelers, and they have these sort of things. It would have been much more costly for him [to intervene.] And I think [Khamenei is] not an idiot, he knows how much endorsing Ahmadinejad in 2009 hurt him and cost him a lot."
Momentum for Rohani grew in the last days of the campaign, reportedly right up to the last moment. A Tehran-based journalist, speaking on condition of anonymity, told RFE/RL that many people did not decide to vote for Rohani until election day. Many people, the journalist said, mobilized and encouraged others to vote for Rohani.
Speaking on June 17 at the Washington-based Stimson Center, Fatemeh Haghighatjou, a prominent former reformist lawmaker, described Rohani's victory as a "welcomed surprise" that created a "win-win situation for all."
"The supreme leader won by seeing a higher rate of participation in order to increase his legitimacy and, for the first time, he extended his call to vote to those who do not believe in the regime itself," Haghighatjou said. "The people won because their vote was counted and we are seeing postelection celebrations. The election showed the Green Movement is alive and, most importantly, the people of Iran spoke out."
Rohani himself gave credit for this victory to the Iranian people.
During his first press conference, held June 17 in Tehran, a female reporter told him he had restored hope to Iran.
"The people brought back hope," replied a smiling Rohani.