SAO PAULO — Jair Bolsonaro, a brash far-right congressman who has waxed nostalgic for Brazil’s old military dictatorship, won the presidency of Latin America’s largest nation Sunday as voters looked past warnings that he would erode democracy and embraced a chance for radical change after years of turmoil.

The former army captain, who cast himself as a political outsider despite a 27-year career in Congress, became the latest world leader to rise to power by mixing tough, often violent talk with hard-right positions. His victory reflected widespread anger at the political class after years of corruption, an economy that has struggled to recover after a punishing recession and a surge in violence.

“I feel in my heart that things will change,” Sandra Coccato, a 68-year-old small business owner, said after she voted for Bolsonaro in Sao Paulo. “Lots of bad people are leaving, and lots of new, good people are entering. There’s a light at the end of the tunnel.”

In Rio de Janeiro, thousands of Bolsonaro supporters gathered on iconic Copacabana Beach, where fireworks went off. In Sao Paulo, Brazil’s largest city, car horns could be heard honking and crowds celebrated as the results came in. There were also reports of clashes between his backers and opponents in Sao Paulo.

Speaking to supporters from his home in Rio, Bolsonaro recounted how he was stabbed while campaigning last month and almost died.

“I was never alone. I always felt the presence of God and the force of the Brazilian people,” he said.

Bolsonaro, who ran on promises to clean up Brazil and bring back “traditional values,” said he would respect the constitution and personal liberty.

“That is a promise, not of a party, not the vain word of a man. It’s a promise to God,” he said, standing next to his wife and many cheering supporters.

Later, he said in a Facebook Live transmission that he had received a call from some world leaders, including U.S. President Trump who wished him good luck.

Addressing supporters in Sao Paulo, his rival, Fernando Haddad of the Workers’ Party, did not concede or even mention Bolsonaro by name. Instead, his speech was a promise to resist.

“We have the responsibility to mount an opposition, putting national interests, the interests of the entire Brazilian people, above everything,” Haddad said. “Brazil has never needed the exercise of citizenship more than right now.”

He later added: “Don’t be afraid. We are here. We are together!”

Brazil’s top electoral court said Bolsonaro won with just over 55 percent of the vote, compared with just under 45 percent for Haddad.

Bolsonaro’s rise was powered by disgust with the political system. In particular, many Brazilians were furious with the Workers’ Party for its role in the mammoth graft scheme uncovered by the “Operation Car Wash” investigation. Haddad struggled to build momentum with his promises of a return to the boom times by investing in health and education and reducing poverty.

Minutes after he was elected, several international human rights groups put out statements demanding that Bolsonaro respect Brazil’s democracy.