Ahmadinejad congratulated Rouhani.
"I have always deeply believed in the vast and endless capacities of the Iranian nation for development and greatness," Ahmadinejad said. "I believe that all peaks of glory can be conquered by believing and trusting in the Iranian nation and by respecting different interests and tastes."
On the streets of Tehran, one celebrating group of two young men and two young women said that if Rouhani does what he promised, they would be "very happy" with him.
The other candidates were two-term Tehran Mayor Mohammad Bagher Ghalibaf, Saeed Jalili, Mohsen Rezaei, Ali-Akbar Velayati and Mohammad Gharazi.
Velayati, Ghalibaf and Jalili, who is Iran's chief nuclear negotiator, are considered close to Khamenei and would have been unlikely to challenge his authority. Of the three, Jalili had enjoyed the most popular support going into the vote.
Results showed that Rouhani secured 18.6 million votes -- or 50.7% of the 36,704,156 votes tallied.
Second was Ghalibaf, with 6.1 million votes, and third was Jalili, with nearly 4.2 million votes.
Moments after Rouhani was declared the winner, supporters started filling the area near Tehran's Haft-e-Tir Square, waving the campaign's purple flags, a witness told CNN. Motorists honked, and pedestrians held their fingers high with the V sign.
Earlier, British Prime Minister David Cameron told CNN's Richard Quest that the international community "will have to deal with whatever the situation is."
"We have to remember this is always only an election between a restricted number of candidates, it's not democracy as we know it," he said.
"We have a very clear message to the Iranian government, which is that there is an option that gets Iran back into the international community, back into the family of nations. But it's got to be proper cooperation on this nuclear dossier, where so little progress frankly has been made. Otherwise, we will continue with the sanctions."
It was Iran's Guardian Council, an unelected body made up of six clerics and six lawyers operating under the oversight of the supreme leader, that drew up the restricted list of candidates from the 680 who initially registered.
Eight candidates were approved, two of whom subsequently dropped out.
The final six contenders didn't include any women. Nor did they include Ahmadinejad's aide and protege Esfandiar Rahim Mashaei, who was among those excluded by the Guardian Council.
Rouhani had the backing of the highly influential former President Akbar Hashemi Rafsanjani and appears to have won over parts of Iran's subdued reform movement.
The 65-year-old cleric's campaign began to gather steam last month, when he dared to accuse the state media of censorship and lies during a live interview on state TV, and then criticized the government's tight grip on security at a televised rally a few days later.
Despite his growing popularity among opposition circles, Rouhani has long been a part of Iran's ruling establishment.
The only cleric among the candidates, he has close ties to Khamenei and served in Iran's parliament for two decades. He was also Iran's lead nuclear negotiator from 2003 to 2005 and holds seats on several powerful decision-making bodies.
Hours into the voting Friday, Rouhani complained of a voting irregularity. A reform candidate, Mohammed Aref, who dropped out of the race earlier in the week, was still on some ballots.
Rouhani was worried that voters might mistakenly select Aref, which would amount to a vote thrown away. It was not clear how many ballot papers were concerned.
Four years ago, when allegations of election fraud sparked widespread protests, Iran's police and the Basij, a feared paramilitary group, cracked down on the opposition Green Movement.
Protesters were jailed, and human rights groups alleged many were tortured and killed behind bars while the government quashed the uprising.
Reform politicians representing the movement, including Ahmadinejad's election rival, former Prime Minister Mir Hossein Moussavi, have been under house arrest since 2011.