Raisi has blamed Rouhani for mismanaging the economy and has travelled to poor areas holding rallies, pledging more welfare benefits and jobs.
"To appeal to change-seeking voters, Rouhani attacked powerful Iranian institutions. whose cooperation he will need in order to govern effectively", said Karim Sadjadpour senior fellow at the Carnegie Endowment who focuses on Iran.
The re-election is likely to safeguard the nuclear agreement Rouhani's government reached with global powers in 2015, under which most worldwide sanctions have been lifted in return for Iran curbing its nuclear programme.
That drew an unusual rebuke directly from the supreme leader, Ayatollah Ali Khamenei, who called the rhetoric at the debate "unworthy". "The fate of the country is in the hands of people", he said.
But his campaign had already started to complain about the conduct of the vote even before polls closed, saying there had been hundreds of "propaganda actions" by Rouhani supporters at voting booths, which are banned under election laws.
Rouhani won by a wide margin, giving the moderate cleric a second four-year term to see out his agenda pushing for greater freedoms and outreach to the wider world.
"I am happy I could vote for Rouhani", said Zohreh Amini, a 21-year-old woman studying painting at Tehran Azad University. "He kept the shadow of war far from our country".
Raisi is widely seen as Khamenei's preferred candidate - indeed, he is often mentioned as his possible successor. He also has history on his side, as all incumbent presidents have won re-election since 1981.
More than 56,400,000 Iranian people were eligible to vote. President Hassan Rouhani, with his reformist policies, may not have been a complete success but he was able to get the nuclear deal signed and economic sanctions eased.
Both candidates urged voters to respect the outcome of the vote.
The two other candidates left in the race, Mostafa Mirsalim, a former culture minister, and Mostafa Hashemitaba, a pro-reform figure who previously ran for president in 2001, respectively have 297,000 and 139,000 votes each.
Relations between the USA and Iran are likely to be remain hostile, according to Karim Sadjadpour of the Carnegie Endowment for International Peace.
Hard-liners remain suspicious of America, decades after the 1953 U.S. -engineered coup that toppled Iran's prime minister and the 1979 U.S. Embassy takeover and hostage crisis in Tehran.
Trump has repeatedly threatened to rip up the deal.
"I congratulate the great victory of the Iranian nation in creating a huge and memorable epic in the continuation of the path of "wisdom and hope", tweeted Vice President Eshaq Jahangiri, referring to the government's slogan.
Six candidates had been vetted and approved by the Guardian Council to run in the election.
In addition to the 12th presidential election and the fifth city and village council election, by-elections are also being held in the cities of Isfahan (in Isfahan), Maragheh, Ahar, Heris (in East Azarbaijan), Ajabshir, Bastak, Parsian and Bandar Lengeh (in Hormuzgan) to elect representatives for a number of vacant parliament seats. But he remains subordinate to the supreme leader, who is chosen by a clerical panel and has the ultimate say over all matters of state.
Reform-minded supporters recognize that Rouhani isn't ideal - he too, after all, is also a cleric.
He promised to heal the wounds of the previous presidential vote in 2009, when mass reformist protests were violently crushed after the disputed the re-election of hardliner Mahmoud Ahmadinejad. Authorities barred Ahmadinejad from running in Friday's election, and Khamenei warned this week that anyone fomenting unrest "will definitely be slapped in the face". Rouhani has been unable to secure the release of reformist leaders from house arrest, and media are barred from publishing the words or images of his reformist predecessor Khatami.