Political analysts say the result of Friday's voting is too close to predict, with deep social divisions apparent in the Islamic republic of 67 million people.
Rafsanjani, 70, bidding to regain the presidency he held from 1989 to 1997, has recast himself as a liberal with vows to extend the reforms of outgoing President Mohammad Khatami, who loosened social rules and pursued detente with the West.
But Ahmadinejad, 48, says ties with Washington are not a priority. He has won over the poor with promises to share out Iran's oil wealth more fairly.
The reform credentials of Rafsanjani have come under fire, with Nobel laureate Shirin Ebadi saying it would make no difference to Iran whoever wins.
In a last-minute bid to win over undecided voters, Rafsanjani offered each Iranian family share options worth about $11,200 in privatised companies if he became president.
In the first round, another candidate came third and just missed the runoff when he offered $62 per month to each Iranian over 18 as a share of Iran's oil wealth.
Supporters of Ahmadinejad plastered streets in the capital with posters in the final hours of campaigning, which officially ended exactly 24 hours before polling stations open at 9am (0430 GMT) on Friday.
Ahmadinejad has vowed to share
Iran's oil wealth more equitably
The election has exposed deep splits among the 47 million eligible voters in Iran, where the minimum voting age is 15.
"Class difference is an issue the revolution was supposed to resolve but didn't. In fact, class disparities have widened despite high oil prices," Karim Sadjadpour, Tehran-based analyst for the International Crisis Group, said.
Rafsanjani's supporters tend to be from the upper and middle classes who are tired of Iran's isolation and want more social freedom. They fear Ahmadinejad will turn back the clock to the strictures and purges that followed the 1979 Islamic revolution.
To them, Ahmadinejad is an outsider challenging the vested business interests of Rafsanjani's wealthy family and others they believe have benefited most from booming oil prices.
Ahmadinejad, a former instructor of the Basij militia, guardians of the revolution's ideals, has support among the working-class, who struggle to make ends meet.
Candidates beaten in the first round and now backing Rafsanjani accuse the Revolutionary Guards and Basij of backing Ahmadinejad.
Electoral laws bar members of the military from campaigning for any particular candidate.
The outgoing president loosened
social rules on his watch
The Interior Ministry has warned that such practices could be repeated in the runoff.
Ahmadinejad has dismissed such charges as smears.
"Say good things about your own candidate instead... It is time to change the management system of the country," he said in a television interview.