In just three years, Luiz Inácio Lula da Silva has gone from prisoner to president-elect.

After being jailed on corruption charges, the left-wing da Silva engineered a stunning political resurrection on Sunday by winning Brazil's presidential runoff election — in a nail-biter — over right-wing incumbent President Jair Bolsonaro.

With nearly all the ballots counted, official returns gave da Silva, who is a former two-term president, 50.8% of the vote compared to 49.2% for Bolsonaro. Da Silva will be sworn-in for a four-year term on Jan. 1.

"I'm really happy," said Victor Costelo, 33, who works in advertising, as he celebrated on the streets of Sao Paulo that were crowded with da Silva supporters, many of them wearing the red colors of his Workers Party. After four years of Bolsonaro, who Costelo described as an authoritarian, he said, "the next four years will be more hopeful for us."

The extremely tight race showed how politically polarized Brazil has become in recent years. Although the balloting was largely peaceful, there were several violent incidents during the campaign with authorities reporting the killings of at least four da Silva supporters at the hands of pro-Bolsonaro fanatics.

Bolsonaro, 67, a populist in the mold of former U.S. President Donald Trump, served as an army captain during Brazil's military dictatorship that lasted from 1964-85 and filled his cabinet with former officers. He repeatedly challenged the legitimacy of the election and the reliability of Brazil's electronic voting machines and hinted that he might not accept the results if he lost.

His anti-democratic rhetoric alarmed many Brazilians, while 77-year-old da Silva promised a return to normality.

"We are going to fix the country and you are going to be happy again," he told voters in the run-up to Sunday's balloting.

Da Silva and Bolsonaro were the two top finishers in a first round of presidential voting on Oct. 2 that featured 11 candidates. But neither da Silva nor Bolsonaro secured more than half the votes required for an outright victory, forcing this weekend's runoff election.

Da Silva has promised to increase the minimum wage and jump-start the economy, which has been flagging since the COVID-19 pandemic hit Brazil and caused nearly 700,000 deaths – the world's second-highest death toll after the U.S. He has also pledged to protect the Amazon rainforest after deforestation hit a 15-year high under Bolsonaro.

Sunday's victory was perhaps the most spectacular in da Silva's roller-coaster political career.

After three failed runs for the presidency, da Silva was elected to the post in 2002 then reelected in 2006. As president, he oversaw an economic boom that helped lift millions out of poverty, making him an icon of the Latin American left.

However, after leaving office, Lula became ensnared in a wide-ranging bribery scandal that landed him in prison for 580 days. His political career appeared to be over. But he was released on a technicality in 2019 and launched yet another run for the presidency that quickly garnered enthusiastic support.

His victory will help consolidate a leftward shift in Latin America where, from Mexico to Argentina, the biggest countries are run by leftist presidents.

Copyright 2022 NPR. To see more, visit https://www.npr.org.