Bolsonaro outperforms the polls as Brazil’s election appears headed to a runoff.

RIO DE JANEIRO — Since Brazil’s high-stakes presidential race began last year, Luiz Inácio Lula da Silva, Brazil’s former leftist president, had led the polls by a wide margin. Many analysts had even predicted he would take the presidency in the first round on Sunday, ousting President Jair Bolsonaro after just one term.

But hours after the polls closed, the two candidates were neck and neck, with more than 79 percent of votes counted. From those results, it appeared nearly certain that the race was heading to a runoff on Oct. 30. Datafolha, one of Brazil’s biggest pollsters, projected late Sunday there was no chance that Mr. da Silva would be elected on Sunday.

The early results were a major victory for Mr. Bolsonaro, who had said for weeks that the polls were severely underestimating his support. So far, the results had suggested he was right.

The contest has been widely regarded as the most important vote in decades for Latin America’s largest nation. That is in part because of the starkly different visions the two men set forth for this country of 217 million people, and because Brazil faces a host of challenges in the years ahead, including environmental threats, rising hunger, a sputtering economy and a deeply polarized population.

But the election has also been widely watched in Brazil and abroad because it has posed a major test for one of the world’s largest democracies. For months, Mr. Bolsonaro has criticized the nation’s electronic voting machines as rife with fraud — despite virtually no evidence — and suggested that the only way he loses is if the election is stolen.

Mr. Bolsonaro, 67, is a far-right populist whose first term has stood out for its turmoil and his constant attacks on the electoral system. He has drawn outrage at home and concern abroad for policies that accelerated deforestation in the Amazon rainforest, for his embrace of unproven drugs over Covid-19 vaccines and for his harsh attacks on political rivals, judges, journalists and health professionals.

Mr. da Silva, 76, is a left-wing firebrand who oversaw Brazil’s boom during the first decade of this century, but then went to prison on corruption charges. Those charges were later thrown out, and now, after leading in polls for months, the man simply known as “Lula” is poised to complete a stunning political resurrection.

They are perhaps the two best-known, and most polarizing, figures in Brazil.

Mr. Bolsonaro wants to sell Brazil’s state-owned oil company, open the Amazon to more mining, loosen regulations on guns and usher in more conservative values. Mr. da Silva promises to raise taxes on the rich to expand services for the poor, including widening the social safety net, increasing the minimum wage, and feeding and housing more people.

The focus on Sunday — when a total of 11 presidential candidates were on the ballot — had been not just on the vote tallies, but also on what would happen after the results were announced.

Mr. Bolsonaro has been casting doubt on the security of Brazil’s electronic voting system for months, claiming without evidence that it is vulnerable to fraud and that Mr. da Silva’s supporters are planning to rig the vote. Mr. Bolsonaro has, in effect, said that the only way he would lose is if the election were stolen from him.

“We have three alternatives for me: Prison, death or victory,” he told supporters at enormous rallies last year. “Tell the bastards I’ll never be arrested.”

Instead, it appeared that by the end of the night, Mr. Bolsonaro would be celebrating.

André Spigariol, Flávia Milhorance and Manuel Andreoni contributed reporting.