State television congratulated Rouhani on his victory. The architect of Iran's still-fragile detente with the West, he led with 58.6 percent of the vote, compared with 39.8 percent for his main challenger, hardline judge Ebrahim Raisi, according to near-complete results broadcast on Saturday.
Although the powers of the elected president are limited by those of unelected Supreme Leader Ayatollah Ali Khamenei who outranks him, the scale of Rouhani's victory gives the pro-reform camp a strong mandate.
Rouhani's opponent Raisi was a protege of Khamenei, tipped in Iranian media as a potential successor for the 77-year-old supreme leader who has been in power since 1989.
The re-election is likely to safeguard the nuclear agreement 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.
And it delivers a setback to the Revolutionary Guards, the powerful security force which controls a vast industrial empire in Iran. They had thrown their support behind Raisi to safeguard its interests.
"I am very happy for Rouhani's win. We won. We did not yield to pressure. We showed them that we still exist," said 37-year-old Mahnaz, a reformist voter reached by telephone in the early hours of Saturday. "I want Rouhani to carry out his promises."
Nevertheless, Rouhani stills faces the same restrictions on his ability to transform Iran that prevented him from delivering substantial social change in his first term and thwarted reform efforts by one of his predecessors, Mohammad Khatami.
The supreme leader has veto power over all policies and ultimate control of the security forces. 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.
"The last two decades of presidential elections have been short days of euphoria followed by long years of disillusionment," said Karim Sadjadpour, senior fellow at the Carnegie Endowment who focuses on Iran.
"Democracy in Iran is allowed to bloom only a few days every four years, while autocracy is evergreen."
The re-elected president will also have to navigate a tricky relationship with Washington, which appears at best ambivalent about the nuclear accord signed by former U.S. president Barack Obama. President Donald Trump has repeatedly described it as "one of the worst deals ever signed", although his administration re-authorised waivers from sanctions this week.
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.
Rouhani, known for decades as a mild-mannered member of the establishment, campaigned as an ardent reformist to stir up the passions of young, urban voters yearning for change. At times he crossed traditional rhetorical boundaries, openly attacking the human rights record of the security forces and the judiciary.
During one rally he referred to hardliners as "those who cut out tongues and sewed mouths shut". In a debate last week he accused Raisi of seeking to "abuse religion for power". The rhetoric at the debate earned a rare public rebuke from Khamenei, who called it "unworthy".
The big turnout appeared to have favoured Rouhani, whose backers' main concern had been apathy among reformist-leaning voters disappointed with the slow pace of change.
Many voters were particularly determined to block the rise of Raisi, one of four judges who sentenced thousands of political prisoners to death in the 1980s, regarded by reformers as a symbol of the security state at its most fearsome.
"The wide mobilisation of the hardline groups and the real prospect of Raisi winning scared many people into coming out to vote," said Nasser, a 52-year-old journalist.
"We had a bet among friends, and I said Raisi would win and I think that encouraged a few of my friends who might not have voted to come out and vote."
The election was important "for Iran's future role in the region and the world", Rouhani said on Friday after voting.
Raisi, 56, had accused Rouhani of mismanaging the economy, travelling to poor areas and holding rallies where he promised more welfare benefits and jobs.
Despite the removal of nuclear-related sanctions in 2016, lingering unilateral U.S. sanctions that target Iran's record on human rights and terrorism have kept foreign companies wary of investing, limiting the economic benefits so far.
(By Parisa Hafezi and Babak Dehghanpisheh. Additional reporting by Bozorgmehr Sharafedin; Writing by William Maclean and Peter Graff Editing by Jeremy Gaunt)
(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)