Final results on Saturday showed Rafsanjani just ahead of the Tehran mayor, followed by cleric Mehdi Karoubi. With no candidate winning a majority of votes, the top two face off in a second round, most likely next Friday.
The success of Ahmadinejad was as stark as the failure of reformist candidate Mustafa Moin to fulfil supporters' hopes that he could repeat outgoing liberal President Mohammad Khatami's landslide success in 1997.
But Karoubi poured scorn on the result of Friday's vote, and made an unprecedented claim that it had been rigged by powerful hardliners in the Revolutionary Guards and Basij militia who backed Ahmadinejad.
With all votes cast in Iran counted, Rafsanjani, a former president who has vowed to expand ties with the West, had polled 21.10% and Ahmadinejad 19.25%. Karoubi was in third place with 17.50% of the vote.
Karoubi came from nowhere in the polls to vault into the top three. He appears to have won support in rural areas which traditionally back clerics after he promised to give all Iranians free monthly handouts of 500,000 rials ($55).
But it was the advance of the former Revolutionary Guard Ahmadinejad - a devotee of supreme leader Ayatollah Ali Khamenei - that was the major shock of the election.
"In our democratic system, liberty is already beyond what could be imagined," he told a victorious post-election news conference.
In fourth place was former police chief Mohammad Baqer Qalibaf with 13.9%. Trailing was Mustafa Moin, the leftist and the main pro-reform candidate, on 13.7%.
Moin's (L) poor preformance was
another blow to the reformists
Moin's result was another dramatic blow for a reform movement already set back by the eight years of dashed hopes that marked Khatami's two terms, where plans to shake up the government were constantly quashed by powerful hardliners.
The run-off between Ahmadinejad and Rafsanjani will present Iran with a stark choice between a conservative and a pragmatist who has taken on an increasingly moderate hue.
As Tehran mayor, Ahmadinejad has banned companies from using Western sports stars such as David Beckham in their advertising and enforced the Islamic dress code.
Rafsanjani, by contrast, has gone out of his way to win over the middle ground, raising the possibility of talks with the US.
"There has been bizarre interference. Money has changed hands," Karoubi said. "They can go and file a lawsuit against me, but I will give all the names of the people in power in my defence."
He appealed to Khamenei to "appoint an honest and trusted committee" to probe the activities of the Guardian Council - an unelected political watchdog - the Interior Ministry, the Revolutionary Guards and the Basij militia.
Ahmadinejad dismissed the allegations as sour grapes, saying "the people who are not chosen in an election always have a tendency to complain."
Around 62% of eligible voters aged 15 and over cast their ballots, the Interior Ministry said, much to the relief of the regime who had looked for strong participation to bolster its legitimacy.
"There has been bizarre interference. Money has changed hands"
For them, the higher-than-expected figure was a response to stinging US criticism of the process and boycott calls from students and prominent liberals such as Nobel Peace Prize winner Shirin Ebadi.
Lies and deceit
While US President George Bush has savaged the vote as ignoring "basic democratic standards", the campaign was marked by an unprecedented use of Westernised promotion methods and airing of once taboo political issues.
Khamenei declared that Iranians had dealt a major defeat to the "lies and deceit" of Bush by turning out en masse in the vote.
"With your solid, collective and glorious presence, once again you defused the conspiracy of your enemy," Khamenei said in a message read out by a state television announcer.