The victory marks a stunning turnaround for the charismatic but tarnished leftist heavyweight known simply as Lula.
He left office in 2010 as the most popular president in Brazilian history but fell into disgrace when he was imprisoned for 18 months on since-quashed corruption charges and now returns for an unprecedented third term at the age of 77.
But, his rival, Mr Bolsonaro, has yet to comment on his loss and has for months been alleging — without providing evidence — that Brazil's electronic voting system is plagued by fraud and that the courts, media and other institutions had conspired against his right-wing movement.
Some supporters of Mr Bolsonaro, 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 Mr da Silva, who had 50.9 per cent of the vote to 49.1 per cent for Mr Bolsonaro with more than 99.9 per cent of polling stations reporting, in the closest race since Brazil returned to democracy after its 1964-1985 dictatorship.
“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,” Mr da Silva told the massive crowd in a victory speech in Sao Paulo.
“This country needs peace and unity,” he said to loud cheers.
“The challenge is immense,” he said of the job ahead, citing a hunger crisis, problems with the economy, bitter political division and deforestation in the Amazon.
Spanish Prime Minister Pedro Sanchez on Monday hailed Mr da Silva’s victory, with Spain's Foreign Minister, Jose Manuel, telling local media that Mr da Silva’s return raised hopes of fighting climate change.
“Lula's triumph is a commitment to the fight against climate change, to biodiversity and also for a special status for the Amazon, which is vital for both,” Mr Manuel told RNE radio.
Under Mr Bolsonaro, deforestation of the Amazon, which decreased sharply under Mr da Silva’s previous term in office, rose sharply. The world's biggest rainforest has until recently helped soak up humanity's soaring carbon emissions but is now strained to the point of starting to release more carbon than it absorbs, research shows.
Mr da Silva on Sunday vowed to “fight for zero deforestation”.
Norway has already announced that it will resume collaboration with Brazil on protection subsidies paid to shield the Amazon, which it halted under Mr Bolsonaro.
Mr Bolsonaro, the hardline conservative, becomes the first incumbent president not to win re-election in the post-dictatorship era.
While he has not publicly accepted the result, officials including the Speaker of the lower house of Congress Arthur Lira publicly backed it.
It is time to “extend a hand to our adversaries, debate, build bridges,” he said.
Congratulations for Mr da Silva poured in from US President Joe Biden, Russian President Vladimir Putin, France's Emmanuel Macron, India's Narendra Modi, Britain's Rishi Sunak and leaders from across Latin America.
The EU's chief diplomat, Josep Borrell, joined the international well-wishers, as did China's foreign ministry spokesman Zhao Lijian and German Foreign Minister Annalena Baerbock.
Mr da Silva's supporters around the country erupted in celebration on Sunday evening.
“We've had four years of a genocidal, hateful government,” said 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 ― dressed in green and yellow, the colours of Brazil's flag which the ex-army captain has adopted as his own ― fell to their knees to pray.
Mr 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 polarising style and attacks on democratic institutions.
Regardless of how the incumbent reacts, Mr da Silva will face huge challenges when he is inaugurated on January 1.
Mr 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, Mr Bolsonaro's former infrastructure minister Tarcisio de Freitas clinched the governorship of Sao Paulo, the most populous and wealthiest state in the country.
And so, Mr da Silva inherits a deeply divided country, with a hugely difficult global economic situation that looks nothing like the commodities “supercycle” that allowed him to lead Latin America's biggest economy through a watershed boom in the 2000s.
Market reactions were mixed on the news, with an exchange-traded fund that tracks Brazilian stocks advancing as much as 5.2 per cent on Monday but international listings of Brazilian firms falling. The US listing of Brazil’s state-run oil company Petrobras suffered some of the biggest declines, dropping by more than 10 per cent in pre-market trading.
Investors are closely watching the possible composition of Mr da Silva's future Cabinet and signs that his rival will question the results.
But events this week could lead to markets stabilising.
“We could see a relief rally as financial markets should react positively to signs of political stability and policies and reforms that would aid investment and growth,” said Stephen Innes, managing partner at SPI Asset Management.