Al Jazeera's Alireza Ronaghi, reporting from Tehran, said this was the most unpredictable and most exciting Iranian presidential election in years because the main contenders each had strong support.
It was clear that Mousavi had a slight edge over Ahmadinejad in the capital, Tehran, but in other provinces it was a totally different story, our correspondent added.
In the final hours of campaigning before the election on Friday, candidates traded bitter accusations.
Ahmadinejad accused his rivals of using "Hitler-style" smear tactics and said they could face jail for insulting the president.
"Such insults and accusations against the government are a return to Hitler's methods, to repeat lies and accusations ... until everyone believes those lies," the semi-official Fars news agency quoted Ahmadinejad as saying.
Insulting senior officials is a crime in Iran punishable by a maximum of two years in prison.
Mousavi, a reformist and former prime minister, accused Ahmadinejad of isolating Iran with his vitriolic attacks on the US and said he lied about the country's economy.
All campaigning was banned from Thursday morning and cars plastered with pictures and campaign material would be stopped and seized, state television reported.
But even after the official end of campaigning, tens of thousands of Mousavi supporters remained in the streets, dancing on cars, waving green flags and passing out pro-Mousavi fliers.
The four candidates for the presidency were cleared by Iran’s Guardian Council, a panel of six senior clerics and six Islamic jurists
The council, which disqualified the rest of the 475 potential candidates who registered, bars women from standing
All Iranians aged over 18 can vote, which means 46 million of Iran's more than 70 million people are eligible
If no candidate wins at least 50 per cent plus one vote of all ballots cast, including blank ones, a run-off round between the two leading candidates will be held on the first Friday after the election result is declared
Mahdi Karroubi, a reformist and former parliamentary speaker, and Mohsen Rezai, a former commander of the Revolutionary Guard, are also standing in Friday's election.
Trita Parsi, the president of the Iranian-American Council, told Al Jazeera that Ahmadinejad's attacks on Mousavi "seems to have backfired and may have motivated the youth to come out and vote, supporting Mousavi's platform of change".
Afshin Molavi of the New America Foundation told Al Jazeera that accusations against Ahmadinejad and the ruling elites of corruption and fat cat insider dealings "will continue to hang in the air long after these elections, and many Iranians know this about the ruling elites".
He said that while Iran has traditionally had high voter turnout, "when we're seeing so many voters than previous polls, it tends to reflect a switch to reformist candidates".
Iran's reformists are hoping that a high turnout on Friday will help them oust the conservative Ahmadinejad, whom they accuse of increasing the country's international isolation and compounding its economic difficulties.
Mousavi's campaign appears to have motivated the youth in a country where one-third of the electorate is under 30 and born after the time of the Islamic Revolution in 1979.
"I believe it is a new beginning and I want to take part in it," Parastou Pazhoutan, a 26-year-old Mousavi supporter, said.
"A month ago, I would have said Ahmadinejad was a sure bet,'' Sharif Emam Jomeh, a political analyst, said.
"There was apathy especially with the youth. But now, until 3am, they are out in numbers and they care ... Below the surface, something was boiling."