The extreme conservative Saeed Jalili lost to the reformist Massoud Pezeshkian in the election to become the next president of Iran.
With 53.3% of the more than 30 million votes counted going to Dr. Pezeshkian, the vote was ruled in his favor. With 44.3% of the vote, Mr. Jalili stood out.
The run-off came after no candidate secured a majority in the first round of the election on 28 June, which saw a historically low voter turnout of 40%.
The election was called after Iran’s previous president Ebrahim Raisi was killed in a helicopter crash in May, in which seven others also died.
Even before the final results were declared by Iran's interior ministry, Dr Pezeshkian's supporters had taken to the streets in Tehran and a number of other cities to celebrate.
Videos posted on social media showed mostly young people dancing and waving the signature green flag of his campaign, while passing cars sounded their horns.
Dr Pezeshkian, a 71-year-old heart surgeon and member of the Iranian parliament, is critical of Iran’s notorious morality police and caused a stir after promising “unity and cohesion”, as well as an end to Iran's “isolation” from the world.
He has also called for “constructive negotiations” with Western powers over a renewal of the faltering 2015 nuclear deal in which Iran agreed to curb its nuclear programme in return for an easing of Western sanctions.
His rival, Saeed Jalili, favours the status quo. The former nuclear negotiator enjoys strong support amongst Iran’s most religious communities.
Mr Jalili is known for his hardline anti-Western stance and opposition to restoring the nuclear deal, which he says crossed Iran’s “red lines".
Turnout in the latest round of voting was 50% - higher than the first round last week, when the turnout was the lowest since the Islamic revolution in 1979 amid widespread discontent, but still considerably low.
Widespread discontent meant that millions of people boycotted the elections.
Lack of choice in the candidates, dominated by Islamic hard liners, and the impossibility of real change as long as the supreme leader tightly controls policies added to their frustration.
Some people who did not vote in the first round were persuaded to cast their ballot for Dr Pezeshkian this time round to prevent Mr Jalili from becoming the president.
They feared that if he won, Iran would be heading for more confrontation with the outside world and that he would bring Iran more sanctions and further isolation.
In order to stand, both candidates had to make it through a vetting process run by the Guardian Council, a body made up of 12 clerics and jurists that hold significant power in Iran.
That process saw 74 other candidates removed from the race, including several women.
The Guardian Council has previously been criticised by human rights groups for disqualifying candidates who are not loyal enough to the regime.
After years of civil unrest - culminating in anti-regime protests that shook the country in 2022-23 - many young and middle-class Iranians deeply mistrust the establishment and have previously refused to vote.
On Iranian social media, the Persian hashtag "traitorous minority" went viral, urging people not to vote for either of the candidates and calling anyone who did a "traitor".
But Supreme Leader Ayatollah Ali Khamenei has rejected suggestions that the low turnout represents a rejection of his rule.
"There are reasons [behind the low turnout] and politicians and sociologists will examine them, but if anyone thinks that those who did not vote are against the establishment, they are plainly wrong," he said.
In a rare move, he acknowledged that some Iranians do not accept the current regime. "We listen to them and we know what they are saying and it is not like they are hidden and not seen," Mr Khamenei said.