Runoff winner Luiz Inacio Lula criticized his nemesis Jail Bolsonaro for not acknowledging the result.
The Supreme Electoral Court (TSE) declared Lula won 50.9% of votes, against 49.1% for Bolsonaro. Lula's inauguration is scheduled for Jan. 1.
"Anyplace else in the world, the defeated president would have called me to recognize his defeat. He hasn't called yet. I don't know if he will," he said in his victory speech to a euphoric sea of red-clad supporters in Sao Paulo.
Pitching the contest as a battle for democracy after his rival made baseless claims that the electoral system was open to fraud, Lula called the election a sign Brazilians "want more and not less democracy," in a victory speech that celebrated what he called his "resurrection."
Bolsonaro is the first Brazilian incumbent to lose a presidential election and Lula has vowed to overturn his legacy, including pro-gun policies and weak protection of the Amazon rainforest.
With some key Bolsonaro allies -- including the speaker of Congress Arthur Lira -- acknowledging the incumbent's defeat, the president did not look to have strong backing in the halls of power to challenge the result.
Lula said he would work to heal a nation wounded by a bitter campaign.
"We'll have to dialogue with a lot of angry people... This country needs peace and unity. The Brazilian people don't want to fight anymore," the ex-metalworker said, his gravelly voice even raspier than usual at the close of a grueling campaign.
Easier said than done, according to political analysts.
"It was a very narrow victory... (that left) half the population unhappy. Lula will have to show a lot of political skill to pacify the country," said political scientist Leandro Consentino of Insper university in Sao Paulo.
"The worst thing that could have happened Sunday was for Brazilians to go to sleep without the president saying anything. It casts doubt over whether he's going to accept the result," he told AFP.
The win was a stunning turnaround for Lula, who left office in 2010 as the most popular president in Brazilian history when he was imprisoned for 18 months on since-quashed corruption charges.
But he is hated by many Brazilians for the economic crisis and massive corruption scandal that marked the end of 13 years in power for his Workers' Party, which crashed to a close when hand-picked successor Dilma Rousseff was impeached in 2016.
Lula faces opposition despite his win. 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.
Lula will face "strong" opposition, and possibly street protests, said political analyst Adriano Laureno of consulting firm Prospectiva.
"He'll take office amid a possible global recession" and difficult economic decisions at home, Laureno told AFP.
"It will be very hard to remain popular," he said.
Lula has acknowledged his tough to-do list.
"The challenge is immense," he said, citing a hunger crisis, weak economy, bitter political division and rampant destruction of the Amazon rainforest."
This report was prepared with information from the Associated Press.