In an election upset in Iran, the reformist candidate who advocated moderate policies at home and improved relations with the West won the presidential runoff against his hard-line rival, the interior ministry said on Saturday.

The reformist candidate, Masoud Pezeshkian, 69, a cardiac surgeon, got 16.3 million votes to defeat Saeed Jalili, with 13.5 million votes. The result delivered a blow to the conservative faction and was a major victory for the reformist camp, which had been sidelined from politics for the past few years.

After polls closed at midnight, turnout stood at about 50 percent, roughly 10 percentage points higher than in the first round with about 30.5 million ballots cast, according to the interior ministry.

The first round had a record low turnout because many Iranians boycotted in protest. But the prospect of a hard-line administration that would double down on strict social rules, including enforcing mandatory hijabs for women, and remain defiant in negotiations to lift international economic sanctions, seemed to have spurred Iranians to turn out.

Mr. Pezeshkian’s supporters took to the streets before dawn on Saturday, according to video footage on social media and his campaign, honking horns, dancing and cheering outside his campaign offices in many cities, including his hometown, Tabriz, when initial results showed he was leading. They also took to social media to congratulate Iranians for turning up at polls to “save Iran,” a campaign slogan of Mr. Pezeshkian’s.

“The end of the rule of minority over majority,” Ali Akbar Behmanesh, a reformist politician and head of Mr. Pezeshkian’s campaign in the province of Mazandaran, said on X. “Congratulations for the victory of wisdom over ignorance.”