Brazil's presidential election is headed for a run-off vote after President Jair Bolsonaro's beat pollsters' predictions against his rival Luiz Inacio Lula da Silva.
With 99.7% of electronic votes counted, leftist Lula was ahead with 48.4% of votes versus 43.3% for the far-right Bolsonaro, the national electoral authority reported.
As neither got a majority of support, the race will go to a second-round vote on October 30.
Several opinion surveys had shown Lula, president from 2003 to 2010, leading Bolsonaro by 10-15 percentage points ahead of Sunday's vote.
The much tighter result dashed hopes of a quick resolution to a deeply polarised election in the world's fourth-largest democracy.
Bolsonaro had questioned polls that showed him losing to Lula in the first round, saying they did not capture the enthusiasm he saw on the campaign trail.
He has also attacked the integrity of Brazil's electronic voting system without evidence, and suggested he might not concede if he lost.
Bolsonaro was calm and confident in his post-election remarks, disparaging polling firms for failing to gauge his support.
"I plan to make the right political alliances to win this election," he told journalists.
Outside Bolsonaro's family home in Rio de Janeiro's Barra da Tijuca neighborhood the mood was upbeat.
Maria Lourdes de Noronha, 63, said only fraud could prevent a Bolsonaro victory, adding that "we will not accept it" if he loses.
"The polls in our country, the media, and journalists, are liars, rascals, shameless," she said.
Bolsonaro's popularity suffered after the coronavirus pandemic, which he called a "little flu" before COVID-19 killed 686,000 Brazilians.
He also dismantled environmental and indigenous protections to the delight of commercial farmers and wildcat miners, while appealing to social conservatives with an anti-gay and anti-abortion agenda.
Although Lula left the presidency 12 years ago with record popularity, he is now disliked by many Brazilians after he was convicted of accepting bribes and jailed during the last election.
His conviction was later overturned by the Supreme Court, allowing him to run again for president this year, along with nine other candidates from an array of smaller parties.
While in power, Lula's approval rating soared as he expanded Brazil's social safety net amid a commodity-driven economic boom.
But in the years after he left office, the economy collapsed, his hand-picked successor was impeached and many of his associates went to prison as part of a vast graft scandal.