Mr Rouhani had a surge of support after endorsement by reformists
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".
A turnout of 72.2% was registered of the 50 million Iranians who were eligible to vote for the successor to Mahmoud Ahmadinejad.
Tehran Mayor Mohammad Baqer Qalibaf was well behind in second place.
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.
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.
Mr 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 UK Foreign Office responded to Mr Rouhani's victory by urging him 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.
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.