|Mr Rouhani casting his vote|
Although the powers of the elected president are limited by those of unelected Supreme Leader Ayatollah Ali Khamenei who outranks him, the scale of Mr Rouhani's victory gives the pro-reform camp a strong mandate.
Mr Raisi, who won 57% of the vote, was tipped in Iranian media as a potential successor for the 77-year-old supreme leader who has been in power since 1989.
The nuclear agreement Mr Rouhani's government reached with global powers in 2015, under which most international sanctions have been lifted in return for Iran curbing its nuclear programme, is now likely to remain safe.
The result also delivers a setback to the Revolutionary Guards, the powerful security force that controls a vast industrial empire in Iran.
|Voters were determined to block the rise of Ebrahim Raisi|
The Supreme Leader has the power to veto all policies and ultimate control of the security forces.
Voters were determined to block the rise of Ebrahim Raisi
The re-elected president will also face the challenge of navigating a tricky relationship with Donald Trump who has dubbed the nuclear agreement signed by former US President Barack Obama as "one of the worst deals ever signed".
Mr Trump arrived on Saturday in Saudi Arabia , his first stop on the first trip abroad of his presidency. The Saudis are Iran's biggest enemies in the region and deplore the nuclear deal.
Mr Rouhani campaigned as an ardent reformist to ignite the passions of young, urban voters yearning for change.
He often crossed traditional boundaries by openly attacking the human rights record of the security forces and the judiciary.
At one rally ally he referred to hardliners as "those who cut out tongues and sewed mouths shut".
Mr Rouhani appeared to have benefited from the big turnout as his backers' main concern had been apathy among reformist-leaning voters disappointed with the slow pace of change.
Many voters were determined to block the rise of Mr Raisi who was one of four judges who sentenced thousands of political prisoners to death in the 1980s.
Ali Vaez, Iran analyst for the International Crisis Group, a think tank said: "Rouhani's vote, particularly in rural areas, shows that Iranian people no longer believe in economic populism and radical change.
"They have the maturity to understand that the solution to their country's predicaments are in competent management of the economy and moderation in international relations.