Rouhani, long known as a cautious and mild-mannered establishment insider, reinvented himself as a bold champion of reform during the election campaign, which culminated on Friday in victory with more than 57 percent of the vote. His main challenger, hardline judge Ebrahim Raisi, received 38 percent.
In his first televised speech after the result, Rouhani appeared to openly defy conservative judges by praising the spiritual leader of the reform camp, former President Mohammad Khatami. A court has banned quoting or naming Khatami on air.
"Our nation's message in the election was clear: Iran's nation chose the path of interaction with the world, away from violence and extremism," Rouhani said.
U.S. Secretary of State Rex Tillerson, whose country has had no diplomatic relations with Iran since 1980, said he hoped Rouhani would use his second term to end Tehran's ballistic missile program and what he called its network of terrorism.
Iran denies any involvement in terrorism and says its missile program, which U.S. President Donald Trump recently targeted with new sanctions, is purely for defense purposes.
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 its strongest mandate in at least 12 years to seek the sort of change that hardliners have thwarted for decades.
Rouhani's opponent Raisi, a protege of Khamenei, had united the conservative faction and had been tipped as a potential successor to the 77-year-old supreme leader. His defeat leaves the conservatives without an obvious flag bearer.
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 program.
And it delivers a setback to the Revolutionary Guards (IRGC), the powerful security force which controls a vast industrial empire in Iran. They had thrown their support behind Raisi to safeguard their interests.
CHEERING AND DANCING
Thousands of people gathered in central Tehran to celebrate Rouhani's victory. Videos on social media showed young people clapping and chanting "We love you Hassan Rouhani, we support you."
Some youngsters wore wristbands in violet, the color of Rouhani's campaign. Others wore green, representing the reformist movement crushed by security forces after a 2009 election, whose leaders have been under house arrest since 2011.
During campaigning, Rouhani promised to seek their release if re-elected with a stronger mandate.
"We won. We've done what we should have for our country. Now it's Rouhani's turn to keep his promises," said coffee shop owner Arash Geranmayeh, 29, reached by telephone in Tehran.
Videos from the cities of Kermanshah, Tabriz and the holy city of Mashhad showed hundreds of people in the streets, cheering and dancing.
Rouhani, 68, faces the same limits on his power to transform Iran that prevented him from delivering social change in his first term, and that thwarted Khatami, who failed to deliver on a reform agenda as president from 1997-2005.
But by publicly thanking "my dear brother, Mohammad Khatami" in his victory speech, Rouhani seemed to take up that mantle. It was a remarkable challenge to the Shi'ite Muslim religious judicial authorities, who have blacklisted Khatami from public life for his support for other reformists under house arrest.
Many experts are skeptical that a president can change much in Iran, as long as the supreme leader has veto power over all policies and control over the security forces. Some said the pattern was all too familiar from Rouhani's first victory four years ago and Khatami's victories the previous decade.
"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 agreed by former U.S. President Barack Obama. Trump has repeatedly described it as "one of the worst deals ever signed", although his administration re-authorized 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 are expected to push hard for Trump to turn his back on the nuclear deal.
Speaking at a joint news conference with his U.S. counterpart in Riyadh, Saudi Foreign Minister Adel al-Jubeir said Iran's presidential election was an internal matter. "We want to see deeds, not words" from Iran, he added.
Kuwait's emir Sheikh Sabah Al-Ahmad Al-Jaber Al-Sabah, an ally of Saudi Arabia, congratulated Rouhani on his re-election.
Rouhani's reinvention as an ardent reformist on the campaign trail helped stir the passion of young, urban voters yearning for change. At times he broke rhetorical taboos, 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 language at the debate earned a rare public rebuke from Khamenei, who called it "unworthy".
The contentiousness of the campaign could make it more difficult for Rouhani to secure the consent of hardliners to carry out his agenda, said Abbas Milani, director of the Iranian Studies program at Stanford University.
"Rouhani upped the ante in the past 10 days in the rhetoric that he used. Clearly it's going to be difficult to back down on some of this stuff."
The Guards could also use their role as shock troops of Iran's interventions across the Middle East to try to derail future rapprochement with the West, said Meir Javedanfar, an Iranian-born lecturer at Israel's Interdisciplinary Center Herzliya.
"Since the 1979 revolution, whenever hardliners have lost a political battle, they have tried to settle scores," he said.
"I would worry about the more confrontational policy of the IRGC in the Persian Gulf ... and more confrontational policy with the U.S. and Saudi Arabia."
Among the congratulatory messages sent to Rouhani by world leaders, Iran's battlefield ally Syrian President Bashar al-Assad looked forward to cooperating "to strengthen the security and stability of both countries, the region and the world".
The biggest prize for Rouhani's supporters is the potential to set Iran's course for decades by influencing the choice of a successor to Khamenei, who has been in power since 1989.
A Raisi victory would have probably ensured that the next supreme leader was a hardliner. Rouhani's win gives reformists a chance to build clout in the body that chooses the leader, the Assembly of Experts, where neither reformists nor conservatives dominate.
Khamenei praised Iranians for their big turnout after voters queued up for hours to cast their ballots. The strong turnout of around 73 percent of eligible voters appeared to have favored Rouhani, whose backers' main concern had been apathy among reformists disappointed with the slow pace of change.
Many voters said they came out 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 mobilization 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."