Brazil's president-elect, the far-right politician Jair Bolsonaro, has promised to defend the country's constitution and unite a bitterly divided electorate.
His left-wing rival meanwhile has vowed to mount a vigorous opposition and rights groups have warned against a rollback of civil liberties.
The two perspectives underscore the reality that the end of the election was not the end of acrimony and that myriad challenges lay ahead for Latin America’s largest nation.
Mr Bolsonaro appeared to try to allay those concerns on 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, while 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 in some of his first words to the nation.
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, Mr Bolsonaro built his popularity on a mixture of tough talk and hardline positions.
And, like many, he is sure to face stiff resistance from groups concerned that his strong views will lead to policies that threaten democratic institutions.
Mr Bolsonaro spent much of the campaign exploiting divisions, taking 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, Mr Bolsonaro’s messages resonated with Brazilians hungry for change.
He received just over 55 per cent of the votes on Sunday, compared to under 45 per cent for Mr Haddad.
The runner-up 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,” Mr 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, Mr Haddad did not congratulate Mr Bolsonaro, whom he had accused of spreading lies about him and his family.
Amid the celebrations by the winner’s supporters, there were also reports of some clashes between his backers and opponents.
The rise of Mr Bolsonaro, who cast himself as a political outsider despite a largely lackluster 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 of 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.
Mr 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 did not help that the man who appointed Mr Haddad as the party's candidate, former President Luiz Inacio da Silva, is serving a 12-year sentence for a corruption conviction.
Mr 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 appoint military men in 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 Mr Bolsonaro's promises about the environment would be "dangerous to the planet".
Mr 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. – Associated Press