SAO PAULO — Far-right congressman Jair Bolsonaro won the presidency of Latin America's largest nation Sunday as voters looked past warnings that the brash former army captain would erode democracy and embraced a chance for radical change after years of turmoil.
With 97 percent of ballots counted, Brazil's top electoral court said Bolsonaro had won with 55.4 percent of the votes. His opponent, leftist Fernando Haddad of the Workers' Party, had 44.4 percent.
In Rio de Janeiro, Bolsonaro's supporters set off fireworks on iconic Copacabana Beach. In Sao Paulo, Brazil's largest city, cars horns could be heard honking and crowds celebrated as the results came in. There were also reports of clashes between his backers and opponents on Avenida Paulista in Sao Paulo.
"I first want to thank God," Bolsonaro told supporters in a video transmitted from his home in Rio, recounting how he was stabbed while campaigning last month and almost died.
Bolsonaro, who ran on promises to clean up Brazil and bring back "traditional values," said he would respect the constitution and personal liberty. He said that wasn't just the words of a man but a promise to God.
Bolsonaro went into Sunday the clear front-runner after getting 46 percent of the vote to Haddad's 29 percent in the first round of the election Oct. 7, which had 13 contenders. After opinion polls in recent weeks had Bolsonaro leading by as much as 18 percentage points, the race had tightened the last few days after several Brazilian heavyweights came out against Bolsonaro, arguing that he was a direct risk to the world's fourth largest democracy.
Bolsonaro, who cast himself as a political outsider despite a 27-year career in Congress, is the latest of several leaders around the globe to gain prominence by mixing tough, often violent talk with hard-right positions. But he is also very much a product of a perfect storm in Brazil that made his messages less marginalized: widespread anger at the political class amid 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," said Sandra Coccato, a 68-year-old small business owner, 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."
Bolsonaro rose in prominence amid disgust with Brazil's political system. In particular, many Brazilians are furious with the Workers' Party for its role in the graft scheme, known as "Carwash" and Haddad struggled to build momentum with his promises of a return to the boom times by investing in health and education and reducing poverty.
Along the way, Bolsonaro also raised serious concerns that he will usher in a rollback of civil rights and a weakening of institutions in what remains a young democracy, especially since he has waxed nostalgic for Brazil's 1964-1985 military dictatorship and said he would name military men to his Cabinet.
In a highly unusual moment, the chief justice of the Supreme Court, Jose Dias Toffoli, read out part of the Constitution to reporters after he voted.
"The future president must respect institutions, must respect democracy, the rule of law, the judiciary branch, the national Congress and the legislative branch," Toffoli said in remarks many took to be a rebuke of Bolsonaro and his more extreme positions.
As late as Sunday morning, Haddad was still holding out hope that he could win after several key endorsements late Saturday.
Among them were a popular former Supreme Court justice, Joaquim Barbosa, who tweeted support for Haddad, saying Bolsonaro's candidacy scared him. Likewise, former Attorney General Rodrigo Janot, one of the biggest crusaders against corruption in Haddad's Workers' Party in recent years, endorsed Haddad for similar reasons.
One of the most important endorsements, particularly for young people, came from Youtuber Felipe Neto, whose channel has nearly 27 million followers.
Neto said he was troubled by Bolsonaro's comments a week ago that "red" leftists would be run out of Brazil.
"In 16 years of the (Workers' Party), I have been robbed, but never threatened," Neto said by Twitter.
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-long recession and is only beginning to emerge, with growth stagnant and unemployment high. And scores of politicians and executives have been jailed in the Carwash corruption investigation that uncovered a multi-billion-dollar scheme to trade public contracts and official favors for bribes and kickbacks.
That instability unleashed extreme anger with the political class but also revealed deep divisions in Brazilian society, and this campaign was the most polarized in decades. There were numerous reports of politically motivated violence, especially directed at gay people, whom Bolsonaro has disparaged. Bolsonaro himself was stabbed and almost died at a campaign event in early September.
"I'm not crazy about Bolsonaro. But he is our way to keep the Workers' Party out of office," said Rafaela Rosa, a 32-year-old teacher, after voting Sunday in Sao Paulo. "We have had enough corruption and now we need to clean up."
Many observers predicted that a newcomer would emerge to harness that anti-establishment anger. Instead, support coalesced around Bolsonaro, who both is and isn't an outsider: He has served for nearly three decades in Congress, but he has often been at the margins of that institution and he painted himself as just the strong man Brazil needed to dismantle a failing system. Bolstering his rebel image is his reputation for offensive statements and sometimes extreme views, including insulting women, gays and blacks.
Bolsonaro's campaign first gained traction with his promises to go after violent crime in a country that leads the world in homicides and where many Brazilians live in daily fear of muggings or burglaries. But his vows to loosen gun laws and give police a freer hand to use force against suspects have also raised concerns that a Bolsonaro presidency could lead to a bloody crackdown and an erosion of civil rights.
The campaign gained momentum by winning over much of the business community with promises of enacting market-friendly reforms that would reduce the size of the Brazilian state, including cutting ministries and privatizing state companies.