It was enough to beat Jair Bolsonaro, whose supporters had been confident of victory.
But the division which this election has highlighted is unlikely to vanish.
It is a stunning comeback for a politician who could not run in the last presidential election in 2018 because he was in jail and banned from standing for office.
He had been found guilty of receiving a bribe from a Brazilian construction firm in return for contracts with Brazil’s state oil company Petrobras.
Lula spent 580 days in jail before his conviction was annulled and he returned to the political fray.
“They tried to bury me alive and here I am,” he said, kicking off his victory speech.
Since the announcement, congratulations have been coming in from leaders around the world, including US President Joe Biden, who noted the result came “following free, fair, and credible elections”. He added he was looking forward to continuing co-operation.
Russian President Vladimir Putin also offered his “sincere congratulations”, saying the results had confirmed Lula’s “impressive political authority”.
India’s Narendra Modi said he looked forward to deepening ties, while UK Prime Minister Rishi Sunak tweeted: “I look forward to working together on the issues that matter to the UK and Brazil, from growing the global economy to protecting the planet’s natural resources and promoting democratic values.”