Political analysts say a win for ultra-conservative Tehran mayor Ahmadinejad, 48, could spell an end to fragile social reforms and harden Iran's foreign policy stance, particularly concerning its nuclear programme.
Rafsanjani, 70, president from 1989 to 1997, has said he wants to improve ties with the West and would prevent "extremism" from monopolising power in the Islamic state.
Although Supreme Leader Ayatollah Ali Khamenei has the final say on all matters of state, analysts say a hard-line presidency 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 are going to inherit a divided nation, both of them are polarising figures."
Voting was extended by four hours to 11.00 pm (1830 GMT) to accommodate a late rush of the 47 million eligible voters.
Interior Ministry officials complained of illegal election-day campaigning and the presence of unauthorised officials at polling stations.
"Some people want to spoil the elections," said ministry spokesman Jahanbaksh Khanjani.
With vote counting just underway, close Rafsanjani aide Mohammad Atrianfar forecast he would win with 55 percent of the vote and said an Ahmadinejad victory would signal fraud, accusing the hard-line Basij volunteer militia of involvement.
"We know that massive irregularities have taken place in steering votes towards a certain candidate in which the Basij has played a role," he told reporters.
Reformist candidates, who lost in the first round, have previously accused the Revolutionary Guards and Basij of campaigning for former Basij member Ahmadinejad, who dismisses such charges. By law, such bodies should be neutral.
An Ahmadinejad aide, who declined to be named, said his candidate was cruising to an easy victory.
"The gap between the candidates in the provinces is so big that even if the Interior Ministry extends the elections for another two days, Ahmadinejad will still be ahead," he said.
Voting was brisk in Ahmadinejad heartlands of support such as south Tehran and the Islamic seminary city of Qom.
Ahmadinejad's humble lifestyle and pledges to attack corruption and redistribute the country's oil wealth have appealed to the urban and rural religious poor, analysts say.
"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.wealthier north Tehran, Rafsanjani voters said they feared Ahmadinejad would reverse modest gains made under outgoing President Mohammad Khatami that now 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. "He also has more political experience."
Reinventing himself as a moderate, Rafsanjani broke many taboos during the campaign. He spoke openly about resuming talks with Washington and discussed sex and fashion in TV adverts.
Rafsanjani and two beaten reformist candidates alleged that Ahmadinejad's shock success came thanks to an orchestrated plot by well-financed hard-line regime elements such as the Guardians Council, the Basij militia and Revolutionary Guards.
But Ahmadinejad, 49, a veteran of the elite Revolutionary Guards, has also gained momentum thanks to his austere image as a God-fearing public servant - appealing to Iranians who suffer unemployment, inflation and corruption.
Rafsanjani, by contrast, is widely believed to be fantastically rich.
The Ayatollah is seen as a moderating force with the regime, using his clout to steer a more pragmatic line in nuclear talks and more open to social freedoms.