SAO PAULO (AP) — Luiz Inácio Lula da Silva has done it again: Twenty years after first winning the Brazilian presidency, the leftist defeated incumbent Jair Bolsonaro Sunday in an extremely tight election that marks an about-face for the country after four years of far-right politics.

With 99.9% of the votes tallied in the runoff vote, da Silva had 50.9% and Bolsonaro 49.1%, and the election authority said da Silva’s victory was a mathematical certainty. At about 10 p.m. local time, three hours after the results were in, the lights went out in the presidential palace and Bolsonaro had not conceded nor reacted in any way.

Before the vote, Bolsonaro’s campaign had made repeated — unproven — claims of possible electoral manipulation, raising fears that he would not accept defeat and would challenge the results if he lost.

The high-stakes election was a stunning reversal for da Silva, 77, whose imprisonment for corruption sidelined him from the 2018 election that brought Bolsonaro, a defender of conservative social values, to power.

“Today the only winner is the Brazilian people,” da Silva said in a speech at a hotel in downtown Sao Paulo. “This isn’t a victory of mine or the Workers’ Party, nor the parties that supported me in campaign. It’s the victory of a democratic movement that formed above political parties, personal interests and ideologies so that democracy came out victorious.”

Da Silva is promising to govern beyond his party. He wants to bring in centrists and even some leaning to the right who voted for him for the first time, and to restore the country’s more prosperous past. Yet he faces headwinds in a politically polarized society where economic growth is forecast to slow and inflation remains high.

This was the country’s tightest election since its return to democracy in 1985, and the first time that a sitting president failed to win reelection. Just over 2 million votes separated the two candidates; the previous closest race, in 2014, was decided by a margin of roughly 3.5 million votes.

The highly polarized election in Latin America’s biggest economy extended a wave of recent leftist victories in the region, including Chile, Colombia and Argentina.

Da Silva’s inauguration is scheduled to take place on Jan. 1. He last served as president from 2003-2010.

Thomas Traumann, an independent political analyst, compared the results to Biden’s 2020 victory, saying da Silva is inheriting an extremely divided nation.

“The huge challenge that Lula has will be to pacify the country,” he said. “People are not only polarized on political matters, but also have different values, identity and opinions. What’s more, they don’t care what the other side’s values, identities and opinions are.”

Congratulations for da Silva — and Brazil — began to pour in from around Latin America and across the world Sunday evening, including from U.S. President Joe Biden, who highlighted the country’s “free, fair, and credible elections.” The European Union also congratulated da Silva in a statement, commending the electoral authority for its effectiveness and transparency throughout the campaign.

Bolsonaro had been leading throughout the first half of the count and, as soon as da Silva overtook him, cars in the streets of downtown Sao Paulo began honking their horns. People in the streets of Rio de Janeiro’s Ipanema neighborhood could be heard shouting, “It turned!”

Da Silva’s headquarters in downtown Sao Paulo hotel only erupted once the final result was announced, underscoring the tension that was a hallmark of this race.

“Four years waiting for this,” said Gabriela Souto, one of the few supporters allowed in due to heavy security.

Outside Bolsonaro’s home in Rio, ground-zero for his support base, a woman atop a truck delivered a prayer over a speaker, then sang excitedly, trying to generate some energy as the tally grew for da Silva. But supporters decked out in the green and yellow of the flag barely responded. Many perked up when the national anthem played, singing along loudly with hands over their hearts.

For months, it appeared that da Silva was headed for easy victory as he kindled nostalgia for his presidency, when Brazil’s economy was booming and welfare helped tens of millions join the middle class.

But while da Silva topped the Oct. 2 first-round elections with 48% of the vote, Bolsonaro was a strong second at 43%, showing opinion polls significantly had underestimated his popularity.

Bolsonaro’s administration has been marked by incendiary speech, his testing of democratic institutions, his widely criticized handling of the COVID-19 pandemic and the worst deforestation in the Amazon rainforest in 15 years. But he has built a devoted base by defending conservative values and presenting himself as protection from leftist policies that he says infringe on personal liberties and produce economic turmoil. And he shored up support in an election year with vast government spending.

“We did not face an opponent, a candidate. We faced the machine of the Brazilian state put at his service so we could not win the election,” da Silva told the crowd in Sao Paulo.

Da Silva built an extensive social welfare program during his tenure that helped lift tens of millions into the middle class. The man universally known as Lula also presided over an economic boom, leaving office with an approval rating above 80%, prompting then U.S. President Barack Obama to call him “the most popular politician on Earth.”

But he is also remembered for his administration’s involvement in vast corruption revealed by sprawling investigations. Da Silva’s arrest in 2018 kept him out of that year’s race against Bolsonaro, a fringe lawmaker at the time who was an outspoken fan of former U.S. President Donald Trump.

Da Silva was jailed for for 580 days for corruption and money laundering. His convictions were later annulled by Brazil’s top court, which ruled the presiding judge had been biased and colluded with prosecutors. That enabled da Silva to run for the nation’s highest office for the sixth time.

Da Silva has pledged to boost spending on the poor, reestablish relationships with foreign governments and take bold action to eliminate illegal clear-cutting in the Amazon rainforest.

“We will once again monitor and do surveillance in the Amazon. We will fight every illegal activity,” da Silva said in his acceptance speech. “At the same time we will promote sustainable development of the communities of the Amazon.”

The president-elect has pledged to install a ministry for Brazil’s original peoples, which will be run by an Indigenous person.

But as da Silva tries to achieve these and other goals, he will be confronted by strong opposition from conservative lawmakers likely to take their cues from Bolsonaro.

Carlos Melo, a political science professor at Insper University in Sao Paulo, compared the likely political climate to that experienced by former President Dilma Rousseff, da Silva’s hand-picked successor after his second term.

“Lula’s victory means Brazil is trying to overcome years of turbulence since the reelection of President Dilma Rousseff in 2014. That election never ended; the opposition asked for a recount, she governed under pressure and was impeached two years later,” said Melo. “The divide became huge and then made Bolsonaro.”

Unemployment this year has fallen to its lowest level since 2015 and, although overall inflation has slowed during the campaign, food prices are increasing at a double-digit rate. Bolsonaro’s welfare payments helped many Brazilians get by, but da Silva has been presenting himself as the candidate more willing to sustain aid going forward and raise the minimum wage.

In April, he tapped center-right Geraldo Alckmin, a former rival, to be his running mate. It was another key part of an effort to create a broad, pro-democracy front to not just unseat Bolsonaro, but to make it easier to govern.

“If Lula manages to talk to voters who didn’t vote for him, which Bolsonaro never tried, and seeks negotiated solutions to the economic, social and political crisis we have, and links with other nations that were lost, then he could reconnect Brazil to a time in which people could disagree and still get some things done,” Melo said.

___

Carla Bridi contributed to this report from Brasilia.