Two days before his 81st birthday, Brazil's president has bucked a wave of Trumpism sweeping across South America to clinch a historic fourth term.
Luiz Inácio Lula da Silva got 51.8 per cent of the presidential vote, while 48.2 per cent went to his opponent Flávio Bolsonaro, the son of former far-right president Jair Bolsonaro.
The results were declared on Sunday, local time, after an unprecedented Supreme Court order to keep polling stations open for an additional two hours was made three minutes before they were set to close.
Lula's campaign requested the extension over claims public officials deliberately slowed public transport and hindered voters in Sao Paulo and Salvador, cities that tend to favour the incumbent.
Bolsonaro's camp also requested an extension in some areas due to heavy rains in Brazil's south, where he is favoured.
But he is now refusing to accept defeat, calling the court's decision a "dirty trick" that "changed the rules in the middle of the game".
"It is impossible to acknowledge the result," Bolsonaro said from Brasilia, as he called for the election to be annulled.
In an address that lasted less than three minutes, Bolsonaro also tried to sow doubt by pointing out that the court order was made by Lula's former lawyer.
His comments echo those of his father Jair Bolsonaro's in 2022, when he refused to recognise his election loss to Lula.
Deborah Barros Leal Farias, an associate professor at the University of NSW, said the decision to extend polling hours was "surprising".
"It added two extra hours of stress, of tension and anxiety and what happened was something that had never happened in Brazilian elections," she said.
It is unclear how many polling stations stayed open after the court order. Some in Brasilia and Rio de Janeiro closed shortly after the original deadline, according to AP reporters on the ground.
But the election day chaos does not provide much basis for a legal challenge, according to Joao Pedro Padua, a law professor at Fluminense Federal University.
"The extension of voting hours is unusual, but for it to invalidate the elections, the candidate would have to prove that it affected voters' ability to freely express their will," he said.
"There doesn't seem to have been any such impact."
Professor Barros Leal Farias agrees.
"Of course, politically, the right is going to say this isn't fair … but I don't think legally they would have a lot of weight," she said, pointing to the fact both candidates asked for polls to stay open.
Lula's unlikely path to victory
Lula's win breaks a string of straight conservative victories across Latin America. Countries including Argentina, Chile, Colombia, and Peru are now governed by Trump-friendly leaders.
It also means the region's largest economy is set to keep defying US demands, despite the pressure of increased tariffs.
Throughout his campaign Lula warned that a Bolsonaro government would hand sovereignty over to Trump, whose State Department campaigned in support of the far-right senator.
"Either the homeland remains free or we die for Brazil," Lula said during his victory speech.
"This country will not accept being anyone's backyard."
Lula is the first candidate to be elected after finishing second in the first round since Brazil returned to democracy in the 1980s.
Pollsters say a better-than-expected turnout (which was 2 per cent higher than the first round) may have given Lula the edge.
"This was nail-bitingly tight … it was quite surprising, because in the first round Flávio won by less than 2 per cent of the vote," Professor Barros Leal Farias said.
"What was really surprising was how Lula's campaign was able to not just reverse the advantage that Flávio had but really gain a significant number of votes."
She said the veteran politician's loss in the first round was a "wake-up call" that prompted a "massive mobilisation on social media as well as on the streets".
"In the very first weekend, for instance, after the first round, were literally millions of Brazilians going to the streets and really galvanising all this energy on behalf of Lula," Professor Barros Leal Farias said.
"Lots of coordinated action, but [also] lots of organic action by people saying, 'Look, if you were the ones who didn't vote or were undecisive, this is why you should choose Lula.'"
The 80-year-old's victory marks another comeback in a political career that has continually defied the odds.
He rose from poverty to become a union leader and ran for president under the Workers' Party three times before wins in 2002 and 2006.
Lula was jailed from 2018 to 2019 on corruption charges that were later overturned, paving the way for him to beat Jair Bolsonaro in the 2022 election.
In his third term he achieved a drop in inflation, unemployment and poverty but the cost of living and crime remained concerns in the country of about 214 million people.
Lula's fourth term could be his most difficult yet, with Bolsonaro's Liberal Party the single largest force in both the lower house and Senate.
Already one of the world's oldest leaders, Lula will be 85 by the end of his new term in 2030.
