* Rouhani, architect of detente, sees off hardline foe
* Revolutionary Guards pinned hopes on his rival Raisi
* High turnout boosted president
* Rouhani thanks reformist leader, defying court ban (Adds Rouhani quotes)
By Parisa Hafezi and Babak Dehghanpisheh
DUBAI/BEIRUT, May 20 (Reuters) - President Hassan Rouhani pledged on Saturday to open Iran to the world and deliver freedoms its people have yearned for, throwing down a defiant challenge to his hardline opponents after securing a decisive re-election for a second term.
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. He promised to serve as president for all Iranians, not just those who voted for him.
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 programme.
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.
"We won. We did what we should do for our country. Now it is Rouhani's turn to keep his promises," said coffee shop owner Arash Geranmayeh, 29, reached by telephone in Tehran.
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 sceptical 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. 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 are expected to push hard for Trump to turn his back on the nuclear deal.
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 ten 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 Centre 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 favoured 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 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." (Additional reporting by Bozorgmehr Sharafedin; writing by Peter Graff; editing by Andrew Roche)