Far-right congressman Jair Bolsonaro has won the race to be the next president of Brazil.

With 96% of the votes counted, the former army captain has 55.5% of the votes while leftist Fernando Haddad of the Workers’ Party has 44.5%, the Supreme Electoral Tribunal said.

Mr Bolsonaro went into Sunday the clear front-runner after getting 46% of the vote to Mr Haddad’s 29% in the first round of the election on October 7, which had 13 contenders.

Brazil Elections
A supporter of presidential front-runner Jair Bolsonaro wears a headband supporting his candidate (Eraldo Peres?AP)

After opinion polls in recent weeks had Mr Bolsonaro leading by as much as 18 percentage points, the race had tightened in recent days as several Brazilian heavyweights came out against him, arguing that he was a direct risk to the world’s fourth largest democracy.

Mr 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 marginalised – 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 Mr 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.”

Brazil Elections
Fernando Haddad had been hoping to spring a surprise in the election (Andre Penner/AP)

Mr 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 a corruption scheme, and Mr Haddad has struggled to build momentum with his promises of a return to the boom times by investing in health and education and reducing poverty.

But Mr Bolsonaro has 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,” Mr Toffoli said in remarks many took to be a rebuke of Mr Bolsonaro and his more extreme positions.