By Asa Fitch and Aresu Eqbali

TEHRAN, Iran -- Moderate Iranian President Hassan Rouhani won re-election by a wide margin Saturday, defeating a hard-line challenger and getting an endorsement for efforts to seek better ties with the West and attract foreign investment to bolster the economy.

In his first term, Mr. Rouhani negotiated a 2015 deal with the U.S. and five other world powers to curb Iran's nuclear program in exchange for an easing of sanctions. During the campaign he pledged to work to have remaining sanctions against Iran lifted as he fights slow growth and high unemployment.

That task could be difficult with President Donald Trump in the White House. Mr. Trump, who before his election promisedto rip up the nuclear agreement, is reviewing his administration's Iran policy and said in April that Iran wasn't living up to the spirit of the deal. Further complicating any diplomacy, Iran refuses to stop ballistic missile tests that drew U.S. sanctions, insisting the program is for defensive purposes.

On Saturday, Mr. Trump started a visit to Saudi Arabia, the first stop on his first trip abroad as president, a sign of warming U.S. ties with Iran's most important regional rival. Saudi Arabia leads a Sunni Muslim alliance aimed at blunting the influence of Shiite Iran.

Mr. Rouhani scored a major victory over Ebrahim Raisi, a conservative cleric and judge close to Iranian Supreme Leader Ayatollah Ali Khamenei. Mr. Rouhani received 57% of the vote, Iran's Interior Ministry said. Mr. Raisi took 38.5%. The rest of the vote was split between two minor candidates.

It was a significantly bigger margin than Mr. Rouhani managed in 2013 when he was elected to his first term with 51%.

In a televised address, Mr. Rouhani highlighted the first-term agenda that swept him back into office and promised more progress in the coming four years.

"Today, the world knows well that the Iranian nation has chosen the path of engagement with the world without violence and extremism, while it is not willing to accept humiliation or threat," he said. "This is the most important message that our nation expects to be heard properly by all governments, neighbors and especially big world powers."

Iranians took to the streets in Tehran after dark to celebrate, marching in disorganized crowds, waving flags, and in some places stopping traffic and dancing in the street. Passing cars honked as revelers chanted the name of Mir Hossein Mousavi, the reformist presidential candidate placed under house arrest after disputed results in the 2009 election.

Mr. Raisi acknowledged his loss in a statement on his official website. He wished Mr. Rouhani success and vowed to fight corruption and strengthen revolutionary values based on the almost 16 million votes he received.

U.S. Secretary of State Rex Tillerson, speaking in Riyadh Saturday with Saudi Foreign Minister Adel al Jubeir, said he hoped Mr. Rouhani's re-election would improve Iran's relations with the rest of the world by shifting its approach to issues of terrorism and human rights.

"We hope that if Rouhani wanted to change Iran's relationship with the rest of the world those are the things he should do," he said.

But in Iran's unique political system, where Mr. Khamenei has final say over state matters and limits the president's power, Mr. Rouhani's convincing victory may not translate into a mandate, according to Ali Vaez, a senior Iran analyst at the Brussels-based International Crisis Group.

"Rouhani's ability to advance his agenda is morea function of his ability to build elite consensus than his margin of victory," he said.

As president, Mr. Rouhani also doesn't control the apparatus that directs Iran's involvement in conflicts overseas, including in Syria and Iraq. That strategy, which has sometimes clashed with U.S. regional policy, is the responsibility of the Islamic Revolutionary Guard Corps, a force accountable only to Mr. Khamenei.

This year's race offered Iranians the choice between two starkly different visions: a technocratic government open to mutually beneficial interaction with the world versus an inward-facing emphasis on self-reliance coupled with populist appeal.

The margin of Mr. Rouhani's win surprised some who saw Mr. Khamenei's acceptance of the vote and Mr. Raisi's loss as an acknowledgment of a sea change in Iranian attitudes. Iran's population skews young, and many in the new generations want to improve Iran's image and embrace culturalfreedoms.

Despite Mr. Khamenei's presence atop Iran's system, presidents are powerful figures who help put in place policies approved by the leader and craft how Iran presents itself to the world. Mr. Khamenei didn't endorse either candidate, though he was critical of Mr. Rouhani's government in the months before the election, complaining about double-digit unemployment and calling for a boost in exports.

Meanwhile, since Mr. Trump took office, the U.S. has slapped two rounds of new sanctions on Iran related to its ballistic missile program. Mr. Rouhani has defended Iran's ballistic missile program, asserting that it is for defensive purposes only.

Mr. Rouhani, however, has no control over the missile program, which is managed by the IRGC. During the second of three presidential debates, Mr. Rouhani criticized the IRGC's test-firing of a missile emblazoned with a slogan calling for Israel's elimination shortly after the nucleardeal took effect.

For Israel and the U.S. and its Arab allies, the election didn't appear likely to alter distrust of Iran's activities in the region. Iran is the main military and financial backer of Syrian President Bashar al-Assad, putting it at odds with U.S. regional policy. It also props up Shiite Muslim militias in Iraq and Hezbollah in Lebanon, a militant group the U.S. has long branded a terrorist organization.

Mr. Rouhani has said he wants to push ahead with attempts to fix his country's economic woes by building trade ties and developing industries at home with help from outside.

French auto makers Renault, Peugeot and Citroën have announced ventures in the country. Boeing Co. and Airbus SE are selling Iran tens of billions of dollars in planes to revamp an aging fleet.

After sowing the seeds in his first term, Mr. Rouhani now has the chance to reap the rewards, said Amir Hossein Karimi, a 25-year-old studentwho volunteered in both of the president's campaigns. He played down Mr. Trump's effect on his plans.

"There are lots of options on the table, but most of them are costly, like war," he said. "I think Trump is a clever man and he won't choose war."

Mr. Raisi, a cleric who served for decades in Iran's opaque judiciary and was appointed by Mr. Khamenei last year as custodian of a multibillion-dollar charity in the holy city of Mashhad, sought to capitalize on the lagging economy. He promised to triple cash handouts for the poor and revive a financially troubled low-income housing project.

Mr. Raisi's increased political prominence suggested he was in the running to succeed the 77-year-old Mr. Khamenei, who has had health problems. With the election loss, Mr. Kupchan said his political career was probably over.

Mr. Raisi acknowledged his loss in a statement on his official website. He wished Mr. Rouhani success and said he would to fight corruption.

Mohmmad Reza Samadi, 19, an electrical engineering student who voted for Mr. Raisi, said he accepted the outcome even if he wasn't happy with it.

"He will go on with the position he has in Mashhad," he said. "So why not come back for the next election?"

Iran's Guardian Council, a vetting body closely overseen by Mr. Khamenei, determined who could appear on the ballot for this election, as it has in the past. The council approved six candidates. Two dropped out, leaving Mr. Rouhani and Mr. Raisi as the only serious contenders.

Write to Asa Fitch at asa.fitch@wsj.com

(END) Dow Jones Newswires

May 20, 2017 14:58 ET (18:58 GMT)

Copyright (c) 2017 Dow Jones & Company, Inc.