TEHRAN—A hard-line Iranian judge opposed to the country’s outreach to the West won Iran’s presidential election, consolidating conservative control over the country’s politics, as Tehran attempts to revive an international nuclear accord that would free the country of some U.S. sanctions.

Ebrahim Raisi received 17.9 million votes, or about 62% of the ballots cast, according to Iran’s interior ministry.

Mr. Raisi’s main challenger, Abdolnaser Hemmati, the only moderate candidate left in the race, received about 8% of the vote.

Iran’s current president, Hassan Rouhani, visited Mr. Raisi on Saturday to congratulate him on the victory.

Iranian President Hassan Rouhani, at lectern, and President-elect Ebrahim Raisi spoke to the media on Saturday.

Iranian President Hassan Rouhani, at lectern, and President-elect Ebrahim Raisi spoke to the media on Saturday.

Photo: official presidential website/Reuters

Mr. Raisi’s victory was largely the result of a historically low turnout of 48.8%—a sign of the divisions within Iranian society and a reflection of widespread discontent with the lack of real choice in the vote.

Millions of Iranians stayed home, partly to protest moves by the clerical establishment to tip the election in Mr. Raisi’s favor before any ballots were cast. In the weeks before the vote, the country’s election supervisory body had disqualified nearly all nonconservative candidates.

Invalid ballots represented about 14% of the total tally, the second-largest share of the vote.

The results also reflect apathy among Iranian voters toward the reformist and moderate candidates. About 2.4 million Iranians, or about 4% of eligible voters, cast ballots for Mr. Hemmati.

By comparison, the politically moderate Mr. Rouhani won 18 million votes in 2013 and 23.5 million in 2017. In the 2009 election, more liberal reformist candidates received 13.5 million votes.

“Many Iranians have lost any hope in reform,” said Roham Alvandi, associate professor of international history at the London School of Economics. “Reformists who called on people to vote were largely ignored.”

With Mr. Raisi’s win, Iran’s main institutions are now controlled by the country’s conservatives, from the presidency and the military to the parliament and judiciary. Such results show Iran shifting toward a one-party state, analysts said.

“This election makes explicit what most Iranians have already known implicitly: that the Islamic Republic will not countenance any peaceful reform,” Mr. Alvandi said. “That erodes what little popular legitimacy the current political system has left.”

Mr. Raisi is due to be sworn in as president in early August.

People lined up to vote in Tehran on Friday.

People lined up to vote in Tehran on Friday.

Daunting challenges await. Iran is facing growing threats and frequent confrontations with Middle East rivals. Tehran is also engaged in tough negotiations with the U.S. over how to revive an international nuclear deal, which Iranians hope would lift some American sanctions that have crushed the Iranian economy.

Iran’s economic troubles have given rise to frequent bouts of unrest. As head of the country’s judiciary, Mr. Raisi has tolerated little dissent, presiding over mass arrests of journalists, lawyers and American citizens, along with death sentences to several minors.

The 60-year-old Mr. Raisi, who is also a cleric, has been part of Iran’s judicial system since the beginning of the Islamic Republic. He is known abroad for serving on a committee that in 1988 condemned thousands of political prisoners to death.

Mr. Raisi studied in the early 1990s under Iran’s Supreme Leader Ali Khamenei and has never appeared to stray far from his positions. In return, Mr. Khamenei has rewarded his protégé with a series of promotions.

After heading Iran’s General Inspection Office, an anticorruption body, for a decade, Mr. Raisi in 2004 became deputy of the country’s judiciary. Mr. Khamenei appointed him prosecutor-general in 2014, and picked him to head the Astan Quds Razavi, a charity worth billions of dollars central to the supreme leader’s business interests.

After Mr. Raisi lost the 2017 presidential election to the incumbent president, Mr. Rouhani, the supreme leader appointed him head of the judiciary in 2019.

Mr. Raisi’s latest advance as president has led analysts to believe Mr. Khamenei is grooming him as his possible successor as supreme leader. “It is not as if he is a heavyweight in any way,” Mehrzad Boroujerdi, director of the School of Public and International Affairs at Virginia Tech, said of Mr. Raisi. “Khamenei had no justification for giving him all these positions, if not to groom him for a high-ranking position, which I think could be to succeed him.”

Mr. Khamenei, 82, was also president before he was picked as supreme leader in 1989. His appointment came after the death of Ayatollah Ruhollah Khomeini.

Even as a cleric in charge of Iran’s judiciary, Mr. Raisi has sought to cast himself as a man of the people. When he ran for president in 2017, he posed for photos with a popular, heavily tattooed rapper, Amir Tataloo, in an attempt to appeal to young voters.

During the final days of his election campaign this year, Mr. Raisi went to a meeting of athletes in Tehran and sat among the crowd to watch parts of the soccer World Cup 2022 qualifier between Iran and Iraq. He took notes when the athletes complained to him about their economic woes, such as poor salaries and benefits.

Earlier in the week, people flocked to his campaign offices with letters asking him for help with their economic problems, similarly to what supporters of former populist President Mahmoud Ahmadinejad did.

How Mr. Raisi will approach Western nations, notably the U.S., depends largely on whom he appoints to his cabinet. He has in the past criticized departing President Hassan Rouhani for pursuing diplomacy, which led to the 2015 international nuclear deal. Although Mr. Raisi has said he supports the nuclear deal, because Mr. Khamenei has endorsed it, he has also said he doesn’t trust the U.S.

More immediately, Mr. Raisi must address a flagging economy while keeping a check on the Covid-19 pandemic.

Growth in Iran has stuttered in recent years, constrained by U.S. sanctions, while unemployment is high and the local currency has plummeted in value. The economic crisis has pushed more families into poverty. It has driven Iranians to protest in the streets, where they have been met with brutal force by security forces.

Supporters of Ebrahim Raisi gathered in Tehran on Wednesday.

Supporters of Ebrahim Raisi gathered in Tehran on Wednesday.

Write to Sune Engel Rasmussen at sune.rasmussen@wsj.com