The ultra-conservative Tehran mayor was earlier reported by officials to be sweeping towards a stunning presidential election victory over veteran cleric Akbar Hashemi Rafsanjani with the backing of Iran's religious poor.
Political analysts say Ahmadinejad's win can spell an end to the fragile social reforms made under outgoing President Mohammad Khatami and harden Iran's foreign policy towards the West, particularly over its nuclear programme.
An official at the Islamic Republic's Guardian Council, which must approve the election results, said that with 13.3 million votes counted, Ahmadinejad had secured 61.6%.
The official said the turnout was 22 million, or 47%, well down on the 63% of Iran's 46.7 million eligible voters, who cast ballots in the first round on 17 June.
Friday's vote exposed deep class divisions in the oil-producing nation of 67 million people.
"Ahmadinejad is well ahead and it seems he is the winner," said an Interior Ministry official, who declined to be named. "Poor provinces have voted massively for Ahmadinejad."
The 48-year-old Ahmadinejad's humble lifestyle and pledges to tackle corruption and redistribute the country's oil wealth have appealed to the urban and rural religious poor, analysts say.
Pro-reform political parties, students, clerics and academics had backed Rafsanjani, accusing Ahmadinejad of representing an authoritarian trend in Iranian politics.
Rafsanjani's aides say voters
were intimidated by his rival
"Ahmadinejad is like a tsunami," a close aide to the mayor said. "In this election, the people were on one side and political parties supporting Rafsanjani were on the other."
Supreme Leader Ayat Allah Ali Khamenei, who has the last word on all state matters, banned supporters from the two sides from holding victory celebrations after a fractious campaign marred by allegations of electoral irregularities.
"It is heard that the campaign staff of the two rivals are preparing to celebrate," he said in a statement. "Dragging people on to the streets ... under any pretext is against the interests of the country."
Earlier, both camps had claimed victory.
Aides to Rafsanjani, who was president from 1989 to 1997 and has cast himself as a reformer, had accused the hardline Basij militia of trying to intimidate voters to back Ahmadinejad.
"We know massive irregularities have taken place in steering votes towards a certain candidate in which the Basij has played a role," one aide, Mohammad Atrianfar, said.
Officials at the reformist-run Interior Ministry also complained of illegal election-day campaigning.
Turnout was 22 million, or 47%
of all eligible Iranian voters
An Ahmadinejad win would represent a crushing political failure for Rafsanjani, 70, who has been at the forefront of Iranian politics for the past 26 years.
Rafsanjani had said he wanted to improve ties with the West.
Ahmadinejad said restoring ties with Washington was unimportant.
US President George Bush's administration accuses Iran of backing terrorism and having a nuclear weapons programme. Tehran denies the charges and says its nuclear programme is solely for power generation.
Although Khamenei has far more power than the president, analysts say a hardline government would remove a moderating influence on decision-making.
"Whoever loses, we are going to feel the reverberations," said Karim Sadjadpour, Tehran-based analyst for think-tank International Crisis Group.
"Either of them is going to inherit a divided nation. Both of them are polarising figures."
Voting was brisk in Ahmadinejad bastions of support such as south Tehran and the Islamic seminary city of Qom.
"Either of them [Ahmadinejad and Rafsanjani] is going to inherit a divided nation. Both of them are polarising figures"
International Crisis Group
"I vote for Ahmadinejad because he wants to cut the hands of those who are stealing the national wealth and he wants to fight poverty ... and discrimination," said Rahmatollah Izadpanah, 41.
In wealthier north Tehran, Rafsanjani voters said they feared Ahmadinejad would reverse modest reforms made under Khatami that allow women to dress in brighter, skimpier clothes and couples to fraternise in public without fear of arrest.
"(Rafsanjani) will prevent society from going backwards and he will give us some freedom," said businessman Morteza, 46.
Earlier on Friday, Aljazeera's correspondent in Iran, Mohamed Hassan al-Bahrani, said regardless of the election's outcome, dealings with the US and other countries would loom large on the Iranian agenda.
He said Rafsanjani believed that Iran's issues were closely linked to its foreign relations while Ahmadinejad asserted that Iran's difficult issues could not be resolved by merely strengthening ties with the US.
As far as economic and domestic policies are concerned, there were no differences between the two candidates, al-Bahrani said. But there were differences in experience and managerial skills.