Luiz Inácio Lula da Silva, Brazil’s leftist former president, defeated conservative incumbent Jair Bolsonaro in the country’s closest presidential race in history on Sunday, cementing Latin America’s shift to the left and marking an extraordinary comeback for a man imprisoned for corruption three years ago.

Mr. da Silva, who last presided over Brazil from 2003 to 2010, secured 50.9% of the electorate to 49.1% for his rival in the runoff, according to electoral authorities, after a marathon of campaign rallies in the country’s poorest corners to appeal to voters hungry for a return to Brazil’s more prosperous past.

Mr. Bolsonaro became the first president in the 25 years since a constitutional amendment made re-election possible to lose. Mr. da Silva’s victory in Brazil, which has a population of 215 million people, means that every major Latin American country, from Argentina to Mexico, will be led by a leftist government when he takes office on January 1.

Mr. da Silva, 77, is the seventh son of illiterate farm workers who later lost a finger in an industrial accident. He has an almost mythical connection to Brazil’s working class and is regarded as an icon by the Latin American left. On the campaign trail, he promised to raise the minimum wage and spend more money on the poor, which were popular proposals among millions of families affected by the Covid-19 pandemic and the global economic crisis.

Mr. da Silva’s victory was greeted with joy by supporters who thronged So Paulo’s main Paulista Avenue, many of whom were draped in the red flag of his Workers’ Party. Mr. da Silva, better known in Brazil and around the world as Lula, was running in his sixth presidential election since 1989, marking his return to power 12 years after leaving office.

Mr. Bolsonaro faced an uphill battle to remain president, despite surprising analysts by receiving 43.2% of the vote in the first round of voting on Oct. 2.  Opponents of Mr. Bolsonaro chastised him for the government’s response to the Covid-19 pandemic, which has killed nearly 700,000 people. According to the Brazilian research group Penssan, many Brazilians blame him for an increase in poverty and hunger, which now affects 33 million people.

Mr. da Silva represented a return to a more prosperous past for many voters. According to the Getulio Vargas Foundation, during his two terms, which ended in 2010, more than 25 million people rose out of poverty. Many Brazilians became homeowners for the first time and splurged on appliances. A commodities boom fueled by China left Brazil flush with cash, allowing it to repay international loans and win the admiration of world leaders, including then-President Barack Obama, who referred to Mr. da Silva as “my man” and called him “the most popular politician on the planet” at a summit.

Germano Silva, a laboratory technician, recalled how, during the da Silva years, he was able to afford to fly for the first time instead of taking a long bus ride. He was able to send three of his children to college thanks to a government scholarship program.

“My vote was one of gratitude,” he said after casting his ballot. “We hope things will get better now, for us, for everyone—there are so many people in need, unemployed, going hungry, people living on the streets.”