TEHRAN, June 18 -- The hard-line, working-class mayor of Tehran will face former president Ali Akbar Hashemi Rafsanjani, a millionaire cleric, in a runoff election for Iran's presidency next week, according to first-round results announced Saturday.

Rafsanjani, who came in first, had been a favorite in recent polls. But the close second-place finish by Mayor Mahmoud Ahmadinejad, who trailed by 400,000 votes, stunned the field of better-known, better-financed candidates.

Two of the candidates charged that militias and uniformed Revolutionary Guards had intimidated voters at polling places on behalf of Ahmadinejad. Mehdi Karrubi, a moderate cleric who finished a close third, appealed to Ayatollah Ali Khamenei to investigate the allegations. Karrubi enjoys friendly relations with Khamenei, who holds the title of Supreme Leader and has ultimate authority in Iran.

The allegation was seconded by campaign aides to Mostafa Moin, the most prominent reformist candidate. Moin finished a distant fifth despite a higher-than-expected turnout that many in the reformist camp thought would improve their standing.

If Ahmadinejad's second-place finish holds, the runoff set for Friday sizes up as a contest between candidates who say that Iran would become a stronger nation by turning outward, as represented by Rafsanjani, and those who see its success as rooted in the religious zeal that served as the foundation for this theocracy, as advocated by Ahmadinejad.

The matchup also carries strong undercurrents of social class conflict.

Rafsanjani, 70, often is called the wealthiest man in Iran. He served two terms as president in the 1980s, a time of often frequent and fierce government crackdowns on human rights. But in wooing the young voters who now dominate Iranian politics, Rafsanjani updated his image as a "pragmatic conservative."

In his $5 million campaign, Rafsanjani vowed to bring an insider's heft to the reformist program of the outgoing president, Mohammad Khatami, to open Iran's cloistered economy to foreign investment and to promote "detente" with the United States, which severed ties with Iran after Islamic radicals took over the U.S. Embassy in 1979. Rafsanjani is "probably the biggest heavyweight figure in the country, whether he wins the presidency or not," said Mohammad Attrianfar, a newspaper publisher who supported him.

Ahmadinejad campaigned as a paragon of modest loyalty to the ideals of the theocracy. His career includes a stint as an instructor for the basij , a militia long feared for its strict enforcement of Islamic social codes and zealous attacks on student demonstrators protesting the closing of newspapers and jailing of professors.

Ahmadinejad, who still wears his beard and hair in the slightly scruffy style of the basij, was elected mayor of the vast capital two years ago, when frustration with Khatami's inability to push through reforms first began to translate into voter apathy. Turnout in the municipal election was barely 10 percent, low enough for the hard-liners' loyal core to prevail.

One of Ahmadinejad's most high-profile acts as mayor was converting the city's cultural centers into religious centers. While stumping for president, he has called for serving the poor as a way to make Iran an example to the Shiite vision of "global Islam." He has expressed less enthusiasm for renewing relations with the United States.

"He's performed very well as mayor," said Ghasem Ajam Majmohammadi, 33, after casting a ballot for Ahmadinejad. "And he's from the lower middle class, so he knows people very well."

In an election season that brought slick, Western-flavored campaigning to Iran, Ahmadinejad's pitch stood out for its austerity. His campaign posters were printed in black and white. A half-hour television special dwelled on the modesty of his home, a traditional Iranian house furnished with only a chair at the desk he shared with his father. "Where's the swimming pool?" the narrator asked. Several voters had described him as refreshingly authentic.

"It's one thing for Tehranis to have an affinity for him, especially south Tehran," a working-class area, said Karim Sadjadpour, resident analyst for International Crisis Group, a Brussels research group. "But in Isfahan? Shiraz? Yazd? He was close to second even before the Tehran votes were counted."

There were indications that hard-liners chose to mobilize for Ahmadinejad to avoid splintering the vote. Three days before the election, Khamenei's representative to the Revolutionary Guard urged loyalists to vote for the candidate with the least pretentious campaign.

"They have mobilized their forces to vote for him," said Farzaneh Firozi, 38, after casting her ballot, for Moin, in Tehran. "Everyone at the polling station was talking about it. All the women with me, all the ones in chadors, were all voting for Ahmadinejad."

The election night drama was fueled by a dispute between the hard-line Guardian Council, one of three clerical bodies that oversee the elected government, and the Interior Ministry, an arm of the elected reformist government. The council's operatives monitor the voting, but the process is officially conducted by the Interior Ministry.

After the polls finally closed at 11 p.m. Friday, vote tallying was proceeding normally in the early hours of Saturday morning. For several hours, the totals reflected preelection polls and exit surveys. With a third of the votes in, Rafsanjani held a firm lead, and Moin was a solid second. But no vote totals were released to the public.

At 5 a.m., Ahmadinejad began to surge ahead. Interior officials expressed quiet surprise, but a Guardian Council spokesman publicly announced the preliminary total.

That sent President Khatami rushing to the Interior building. Before cameras, he upbraided the Guardian Council for butting in.

At a news conference, Karrubi declared, "Some centers of power are violating the law and are trying to get more votes for a particular person with the help of the Guardian Council.

Interior and Guardian Council officials also differed on turnout. While interior officials slowly moved their estimate toward 60 percent, a Guardian official claimed it approached 70 percent -- comparable to the turnout when Khatami was reelected four years ago.

The turnout figure is of intense interest to Iran's clerical establishment, which casts voter participation as an endorsement of the system it guides. Some reform advocates had supported a boycott of the vote, saying such a move would highlight the need for fundamental changes in a system whose top leaders are unelected.

The stakes grew even higher this week after President Bush issued a statement on the eve of the vote saying that Iran's electoral process "ignores the basic requirements of democracy." State television cast the White House statement as a taunt, saying Bush had declared that the vote of Iranians did not count. Numerous voters said the statement prompted them to vote.

"The timing was poor," Sadjadpour, the analyst, said of the statement. "It was spun here, and it really benefited the regime."

Karrubi's strong showing surprised many observers. The moderate cleric was speaker in the last reformist parliament, but lost his reelection bid and barely registered on preelection polls for president.

One analyst said the likely reason for Karrubi's strong finish was the cash offer he made the centerpiece of his campaign: $60 a month to every citizen older than 18, in a country where the economy is by far the top issue and per capita income is $2,000 a year.

"People were ashamed of saying they would vote for someone for that reason," the analyst said on condition of anonymity because his employer had not authorized him to speak. "People lied."