The victory marks a stunning turnaround for the charismatic but tarnished leftist heavyweight, who left office in 2010 as the most popular president in Brazilian history, fell into disgrace when he was imprisoned for 18 months on controversial, since-quashed corruption charges, and now returns for an unprecedented third term at age 77.
All eyes will now be on how Bolsonaro and his supporters react to the result after months of alleging -- without evidence -- that Brazil's electronic voting system is plagued by fraud and that the courts, media and other institutions had conspired against his far-right movement.
"This country needs peace and unity," Lula said to loud cheers in a victory speech in Sao Paulo.
"The challenge is immense," he said of the job ahead, citing a hunger crisis, the economy, bitter political division, and deforestation in the Amazon.
He later addressed a tightly packed crowd of hundreds of thousands of supporters who flooded the city center clad in Workers' Party red, vowing: "democracy is back."
'He hasn't called yet'
Bolsonaro, 67, was silent in the hours after the result was declared.
"Anywhere in the world, the losing president would already have called to admit defeat. He hasn't called yet, I don't know if he will call and concede," Lula told the massive crowd.
Some Bolsonaro supporters, gathered in the capital Brasilia, refused to accept the results.
"The Brazilian people aren't going to swallow a faked election and hand our nation over to a thief," said 50-year-old teacher Ruth da Silva Barbosa.
Electoral officials declared the election for Lula, who had 50.9 percent of the vote to 49.1 percent for Bolsonaro with more than 99.9 percent of polling stations reporting, in the closest race since Brazil returned to democracy after its 1964-1985 dictatorship.
Bolsonaro, the vitriolic hardline conservative dubbed the "Tropical Trump," becomes the first incumbent president not to win re-election in the post-dictatorship era.
With no word from Bolsonaro, some of his key allies appeared in public to accept the results. They included the speaker of the lower house of Congress, Arthur Lira, who said it was time to "extend a hand to our adversaries, debate, build bridges."
Congratulations for Lula poured in from US President Joe Biden, France's Emmanuel Macron, and Russian President Vladimir Putin, as well as leaders from across Latin America.
The European Union's chief diplomat, Josep Borrell, also joined the list of international well-wishers.
Lula supporters around the country erupted into celebration Sunday evening.
"We've had four years of a genocidal, hateful government," said Lula voter Maria Clara, a 26-year-old student, at a victory party in downtown Rio.
"Today democracy won, and the possibility of dreaming of a better country again."
In Brasilia, the tearful crowd of Bolsonaro supporters -- outfitted in green and yellow, the colors of Brazil's flag which the ex-army captain has adopted as his own -- fell to their knees to pray.
Bolsonaro surged to victory four years ago on a wave of outrage with politics as usual, but came under fire for his disastrous handling of the Covid-19 pandemic -- which left more than 680,000 dead in Brazil -- as well as a weak economy, his polarizing style and attacks on democratic institutions.
Regardless of how the incumbent reacts, Lula will face huge challenges when he is inaugurated on January 1.
Bolsonaro's far-right allies scored big victories in legislative and governors' races in the first-round election on October 2, and will be the largest force in Congress.
On Sunday, Bolsonaro's former infrastructure minister Tarcisio de Freitas clinched the governorship of Sao Paulo, the most populous and the wealthiest state in the country.
In his victory speech, Lula touched on gender and racial equality and the urgent need to deal with a hunger crisis affecting 33.1 million Brazilians.
"Today we tell the world that Brazil is back," he said, adding that the country is "ready to reclaim its place in the fight against the climate crisis, especially the Amazon."
He vowed to "fight for zero deforestation."
Lula inherits a deeply divided country, with a hugely difficult global economic situation that looks nothing like the commodities "super-cycle" that allowed him to lead Latin America's biggest economy through a watershed boom in the 2000s.
Lula's win is "one of the biggest comebacks in modern political history," tweeted Brian Winter, editor-in-chief of Americas Quarterly.
But the president-elect will face a hostile Congress and have "a weak government," Winter told AFP.
None of that mattered for the time being to elated Lula supporters.
"Brazil is starting to stand upright again after four years of darkness. We were going through so many problems, so much fear," Larissa Meneses, a 34-year-old software developer, told AFP at a joyful victory party in Sao Paulo.
"Now with Lula's victory, I really believe things will start getting better. This is a day to laugh a lot."