Jair Bolsonaro, 63, a long-time congressman, will take office on January 1.

Right wing former army captain Jair Bolsonaro was elected president of Brazil on Sunday after beating leftist opponent Fernando Haddad in a second round election run-off at the end of a bitter and highly polarized campaign.

Supporters flooded the streets outside Bolsonaro's home in Rio de Janeiro, letting off fireworks and waving Brazilian flags.

Official results after polls closed gave the controversial 63-year-old Bolsonaro 55.7 percent of the vote with more than 88 percent of the ballots counted as his vow to rescue the country from crisis with a firm grip resonated with Brazil's 147 million registered voters.

Supporters flock the streets after the announcement of Jair Bolsonaro's victory.

He will take office on January 1, 2019.

Despite repulsing many with his open support of the torture used by Brazil's former military dictatorship, as well as remarks deemed misogynist, racist and homophobic, Bolsonaro managed to tap into deep anti-establishment anger that propelled him to victory.

Haddad, a 55-year-old former Sao Paulo mayor, had been making up ground after finishing a distant second to Bolsonaro in the first round three weeks ago with just 29 percent compared to 46.

But despite reducing an 18-point deficit two weeks ago to eight to 10 points in an opinion poll published on Saturday, it wasn't enough for the Workers Party candidate picked to replace the jailed former president Luiz Inacio Lula da Silva.

"After decades, Brazil finally has a chance to elect a president who truly represents Brazilians' values," Bolsonaro had tweeted on Saturday, in his final pitch to voters.

Bolsonaro conducted his entire campaign on social media since an attacker stabbed him in the stomach at a rally in early September, sending him to hospital for three weeks.

On Sunday, he voted at a military academy in Rio de Janeiro, ducking in through a side door to avoid the waiting crowd.

Wearing an army-green jacket, he left with a double thumbs-up, saying only that he could not make a statement for security reasons.

Jair Bolsonaro was seen showing a heart symbol post the presidential vote result.

'Democracy at risk'

"Democracy is at risk, individual freedom is at risk," Haddad, 55, had warned after casting his ballot at a school in Sao Paulo, thronged by supporters clutching red and white roses -- who briefly scuffled with opponents banging pots and pans in protest.

Bolsonaro outrages a large part of the electorate -- and many outside the country -- with his overtly misogynistic, homophobic and racist rhetoric.

He once told a lawmaker he opposed that she "wasn't worth raping;" he has said he would rather see his sons die than come out as gay; and he commented after visiting one black community that they "do nothing -- they're so useless I doubt they can procreate."

But an even larger portion of voters reject Haddad and the tarnished legacy of his Workers' Party.

The Latin American giant's election comes on the heels of its worst-ever recession, a staggering multi-billion-dollar corruption scandal and a year of record-setting violent crime.

Haddad stood as a surrogate for the popular -- but disqualified -- Lula, who led Brazil through the boom years of 2003 to 2010, before both the country and his left-wing political project went bust.

The highly controversial Lula, who stands accused of masterminding the massive pilfering of state oil company Petrobras, is serving a 12-year sentence for bribery.

Lacking his mentor's charisma, Haddad struggled to unite opposition to Bolsonaro, despite mounting fears over what the former army officer's presidency would bring.

One election, two Brazils

Bolsonaro, a veteran congressman, is unabashedly nostalgic for Brazil's brutal 1964-85 military dictatorship, and has been accused of authoritarian tendencies.

But his law-and-order message has resonated.

"We're electing our captain. He's going to sweep out all the corrupt politicians, the crooks, the communists," said supporter Alvaro Cardoso, 55, at the polling station where Bolsonaro cast his ballot, wearing a black T-shirt with the candidate's name and a skull.

Feelings on the other side were equally strong.

"As a black and LGBT woman, I'm frightened" by Bolsonaro, said Luiza Rodrigues Santana, a 23-year-old student in Brasilia.

"He incites hatred. If he becomes president, he'll legitimize all that hatred inside people."

Supporters of far-right presidential candidate Jair Bolsonaro, celebrate in front of his house in Rio de Janeiro

Rejection vote

The election was decided as much by Brazilians voting against something as for it.

"I don't really like either candidate," Elias Chaim, 23, an engineering student and music producer, told AFP at a polling station facing Rio's Copacabana beach.

But he voted for Haddad because of Bolsonaro's "discourse of hate and intolerance."

In Sao Paulo, the economic capital, Marcos Kotait, 40, a publicist, said he had "never seen such a polarized election."

"It used to be people would actually vote for what they wanted, and not just against something," he said.

Outgoing President Michel Temer, himself implicated in corruption, is set to leave office on January 1 as the most unpopular president in Brazil's modern democracy.

The center-right leader took over from Lula's successor, Dilma Rousseff, after she was impeached in 2016 for financial wrongdoing in office, ending the Workers' Party's 13 years in power.