SAO PAULO -- In some of his first words to the nation as president-elect, far-right politician Jair Bolsonaro promised to defend the constitution and unite a bitterly divided populace.

His left-wing rival immediately vowed to mount a vigorous opposition, while rights groups warned against a rollback of civil liberties.

That juxtaposition underscored that the end of the election was not the end of acrimony and that myriad challenges lay ahead for Latin America's largest nation.

Bolsonaro appeared to try to allay those concerns Sunday night, saying he would "pacify" Brazil following a race that revealed deep divisions and was repeatedly marred by violence. The candidate himself was stabbed and almost died while campaigning in early September, and there were numerous reports of politically motivated violence, especially directed at gay people.

"This country belongs to all of us, Brazilians by birth or by heart, a Brazil of diverse opinions, colours and orientations," he said, reading off a sheet of paper in a live television address.

His victory moved Brazil, the world's fourth-largest democracy, sharply to the right after four consecutive elections in which candidates from the left-leaning Workers' Party won.

Like other right-leaning leaders who have risen to power around the globe, Bolsonaro built his popularity on a mixture of tough talk and hardline positions. And, like many, he is sure to face stiff pushback from groups concerned that his strong views will lead to policies that threaten democratic institutions.

Bolsonaro has frequently disparaged women, gays and blacks, praised torture and killings by police, and has said he would name military men to his Cabinet.

He often took to Twitter to lambaste the rival Workers' Party as unethical and dangerous. In recent weeks, Brazilians were bombarded with WhatsApp messages that condemned Workers' Party candidate Fernando Haddad, often making outrageous claims.

Ultimately, Bolsonaro's messages resonated with Brazilians hungry for change: He got just over 55 per cent of the votes Sunday, compared to right under 45 per cent for Haddad.

Haddad promised a fight while saying he would respect the country's institutions.

"We have the responsibility to mount an opposition, putting national interests, the interests of the entire Brazilian people, above everything," Haddad said in a speech to supporters. "Brazil has possibly never needed the exercise of citizenship more than right now."

In a sign of how tense the race got, Haddad did not congratulate Bolsonaro, whom he had accused of spreading lies about him and his family.

Amid the celebrations by Bolsonaro's supporters, there were also reports of some clashes between his backers and opponents.

Among international leaders congratulating Bolsonaro was U.S. President Donald Trump, who tweeted Monday that he had "" with Brazil's president-elect.

"We agreed that Brazil and the United States will work closely together on Trade, Military and everything else!," Trump tweeted. "Excellent call, wished him congrats!"

The rise of Bolsonaro, who cast himself as a political outsider despite a largely lacklustre 27-year career in Congress, parallels the emergence of hard-right leaders in many countries. But his extreme messages were rendered more palatable by a perfect storm in Brazil: widespread anger at the political class after years of corruption, an economy that has struggled to recover from a punishing recession and a surge in violence.

In particular, many Brazilians were furious with the Workers' Party for its role in the graft scheme revealed by the "Operation Car Wash" investigation, which uncovered billions of dollars in bribes paid to politicians via inflated construction contracts.

Haddad struggled to build momentum with his promises of a return to the boom times by investing in health and education and reducing poverty. It didn't help that the man who appointed Haddad as the party's candidate, former President Luiz Inacio da Silva, is serving a 12-year sentence for a corruption conviction.

Bolsonaro's candidacy raised serious concerns that he would roll back civil rights and weaken institutions in what remains a young democracy. He has frequently disparaged women, gays and blacks, and said he would name military men to his Cabinet.

Within minutes of his victory being declared, international civil rights groups expressed concerns. Human Rights Watch called on Brazil's judiciary and other institutions to "resist any attempt to undermine human rights, the rule of law and democracy under Jair Bolsonaro's government."

Steve Schwartzman of the Environmental Defence Fund warned that Bolsonaro's promises about the environment would be "dangerous to the planet."

Bolsonaro has repeatedly said he would pull Brazil from the Paris agreement on climate change, though last week he backed off on that. He has also promised to cut environmental regulations and make Brazil friendlier to international investors.

The past few years in Brazil have been exceptionally turbulent. In 2016, then-President Dilma Rousseff of the Workers' Party was impeached and removed from office on charges that many on the left felt were politically motivated. The economy suffered a two-year recession and is only now beginning to emerge, with growth stagnant and unemployment high.

Dozens of politicians and executives have been jailed in the corruption investigation.

Many observers had predicted a newcomer would emerge to harness the anti-establishment anger. Instead, support coalesced around Bolsonaro, who at the margins in Congress painted himself as just the strong man Brazil needed to dismantle a failing system.

Bolsonaro has promised to crack down on violent crime and drastically reduce the size of Brazil's state. But many of the details of his positions remain unclear since he has largely conducted his campaign via blasts on social media. After he was stabbed, he declined to debate Haddad and gave interviews only to largely friendly media who rarely asked tough questions.

"Tomorrow we start a new country," said Mario Marcondes, a 61-year-old pilot who gathered with other supporters outside Bolsonaro's house. "This was like a soccer game, a dispute that is over. Now we will all know to unite."

------

Associated Press writer Prengaman reported this story from Rio de Janeiro and AP writers Sarah DiLorenzo and Mauricio Savarese reported in Sao Paulo. Associated Press reporter Marcelo Silva de Sousa also contributed to this report.