Reformist-backed cleric Hassan Rouhani has won Iran’s presidential election, securing just over 50% of the vote and so avoiding the need for a run-off.
Crowds gathered in Tehran to hail Mr Rouhani, who said he had achieved a “victory of moderation over extremism”.
Some 72.2% of the 50 million eligible Iranian voters cast ballots to choose the successor to Mahmoud Ahmadinejad.
The US said it was “ready to engage directly” with Iran over its disputed nuclear programme.
Supreme Leader Ayatollah Ali Khamenei congratulated Mr Rouhani on his victory.
“I urge everyone to help the president-elect and his colleagues in the government, as he is the president of the whole nation,” he said.
Ayatollah Khamenei will ratify the vote on 3 August and the new president will then take the oath in parliament.
Mr Rouhani, who has pledged greater engagement with Western powers, said: “This victory is a victory for wisdom, moderation and maturity… over extremism.”
But he also urged the world to “acknowledge the rights” of Iran.
He said: “The nations who tout democracy and open dialogue should speak to the Iranian people with respect and recognise the rights of the Islamic republic.”
Interior Minister Mostafa Mohammad Najjar announced that Mr Rouhani had won 18,613,329 of the 36,704,156 votes cast. This represented 50.71% of the vote.
Tehran Mayor Mohammad Baqer Qalibaf won 6,077,292 votes to take second place (16.56%).
Saeed Jalili came third and Mohsen Rezai fourth.
Mr Najjar said that any challenge by presidential candidates would have to be lodged to the Guardian Council within three days.
The winning candidate needed more than 50% of all ballots cast, including invalid ones, to avoid a run-off.
Crowds gathered in Vali-Asr Square in central Tehran and in Kaj Square in the north-west of the capital to hail the victory.
“Long live reform, long live Rouhani,” members of the crowd chanted.
One of them told Reuters news agency: “Many people are holding Rouhani posters. Some are hugging and crying. We are all so happy here. We can’t believe there is finally a change.”
The US said it respected the vote, although White House spokesman Jay Carney cited concern at censorship and lack of transparency.
The US would “engage Iran directly” to find a “diplomatic solution that will fully address the international community’s concerns about Iran’s nuclear programme”.
The UK Foreign Office urged Mr Rouhani to “set Iran on a different course for the future: addressing international concerns about Iran’s nuclear programme… and improving the political and human rights situation for the people of Iran”.
France said it was “ready to work” with the new leader.
One of Mr Rouhani’s main pledges was to try to ease international sanctions imposed on Iran over its nuclear programme.
Iran has been suffering economic hardship, with rising unemployment, a devalued currency and soaring inflation.
BBC diplomatic correspondent Jonathan Marcus says Mr Rouhani’s election is not necessarily the moment for a substantive change in Iran’s policy, but that even a change in style might offer an opening to the US and key UN Security Council members which would be worth testing for any real sign of flexibility in Tehran.
Voting had been extended by five hours on Friday evening to allow more people to cast their ballots.
Although all six candidates were seen as conservatives, analysts say Mr Rouhani – a 64-year-old cleric often described as “moderate” who has held several parliamentary posts and served as chief nuclear negotiator – has been reaching out to reformists in recent days.
The surge of support for him came after Mohammad Reza Aref, the only reformist candidate in the race, announced on Tuesday that he was withdrawing on the advice of pro-reform ex-President Mohammad Khatami.
Mr Rouhani thus went into polling day with the endorsement of two ex-presidents – Mr Khatami and Akbar Hashemi Rafsanjani, who was disqualified from the race by the powerful Guardian Council, a 12-member body of theologians and jurists.
The hardline candidates included Mr Qalibaf – who is seen as a pragmatic conservative – and nuclear negotiator Mr Jalili – who is said to be very close to Ayatollah Khamenei.
The other three candidates were Mr Rezai, a former head of the powerful Revolutionary Guards, former Foreign Minister Ali Akbar Velayati, and former Telecommunications Minister Mohammad Gharazi.
After the last presidential election in June 2009, millions of Iranians took to the streets to demand a rerun, when the supreme leader dismissed claims by the three defeated candidates of widespread fraud.
No foreign observers monitored this year’s election and there have also been concerns that media coverage in the run-up has been unfair.
Many reformist newspapers have been shut down, access to the internet and foreign broadcasters has been restricted, and journalists have been detained.