Rouhani, a 68-year-old moderate cleric who spearheaded a 2015 nuclear deal with world powers, said Iranians had chosen the "path of engagement" and rejected extremism after he took 57 percent of the vote against 38 percent for hardline cleric Ebrahim Raisi.
"The Iranian people want to live in peace and friendship with the rest of the world, but will not accept any threat or humiliation," Rouhani said on state television.
But he faces a stark challenge from US President Donald Trump, who is currently visiting Iran's bitter regional rival Saudi Arabia, and has threatened to tear up the nuclear deal and reimpose sanctions.
US Secretary of State Rex Tillerson said a $110-billion arms deal with the Saudis was aimed at helping the kingdom deal with "malign Iranian influence", and called on Rouhani to dismantle Tehran's "network of terrorism" and end ballistic missile testing.
The arms package "supports the long-term security of Saudi Arabia and the entire Gulf region", Tillerson told reporters in Riyadh.
Rouhani made an apparent dig at Saudi Arabia, saying Iran had demonstrated to its neighbours that "the path to ensuring security is the reinforcement of democracy, not relying on foreign powers".
In Tehran and across Iran, there were jubilant scenes as thousands of Rouhani supporters took to the streets, singing and dancing.
They wore the purple colours of Rouhani's campaign, as well as green to signify the reformist protests that were brutally put down in 2009.
Police tried to disperse people in central Vali Asr Square, saying they did not have a permit to assemble, but the crowd was too large to manage.
"We've achieved what I wanted, which was not Mr Rouhani himself, but the path of reform, freedom and progress," said Pegah, a 25-year-old in the crowd.
A massive 73-percent turnout forced election officials to extend voting by several hours on Friday.
Raisi, 56, had cast himself as a defender of the poor and called for a tougher line with the West, but his revolutionary rhetoric and promise of increased handouts gained limited traction.
The result "shows that Iranian people no longer believe in economic populism and radical change," said Ali Vaez, Iran analyst for the International Crisis Group think tank.
Putin, Assad congratulate
Russia, which fights alongside Iran in support of Syrian President Bashar al-Assad, was among the first to congratulate Rouhani.
President Vladimir Putin sent a telegram confirming "his readiness to continue active joint work... in line with maintaining stability and security in the Middle East and the world," the Kremlin said.
Assad also praised Rouhani for earning "the trust (of) the Iranian people".
European and Asian officials welcomed Rouhani's re-election, which creates extra space for business opportunities after the nuclear deal lifted global sanctions in exchange for curbs to Iran's atomic programme.
"A lot of investors I hadn't heard from for three months were suddenly phoning me this morning. Some are already in the process of booking their tickets," said Farid Dehdilani, international adviser for the Iranian Privatisation Organisation.
EU foreign policy chief Federica Mogherini tweeted her vow to work for greater "engagement, regional peace... (and the) expectations of people in Iran".
Britain and France also congratulated Rouhani, praising the high turnout but also urging the president to continue his support for the nuclear deal.
- 'Era of extremism over' -
Rouhani put civil liberties at the heart of his campaign, and he pointedly thanked reformist former president Mohammed Khatami, banned from appearing in the media since the 2009 protests, in his acceptance speech.
Rouhani also pushed boundaries over the past fortnight with his veiled criticism of the country's conservative-dominated judiciary and security services, telling supporters: "We've entered this election to tell those practising violence and extremism that your era is over."
But the economy remains Rouhani's top priority.
"Dr Rouhani will more aggressively pursue his economic agenda -- investing in factories, production and absorbing foreign capital," said Dehdilani.
Rouhani tamed inflation and the nuclear deal led to a rebound in oil revenues, but the wider economy is stagnant, with unemployment running at 12.5 percent overall, and almost 30 percent among young people.
"Not everyone who voted for Rouhani thought his record was excellent, but the majority decided his competitor would be even worse," said Tehran University political professor Foad Izadi.
(This story has not been edited by NDTV staff and is auto-generated from a syndicated feed.)