Far-right candidate Jair Bolsonaro won Brazil’s presidential election Oct. 28, marking a dramatic shift to the right in Latin America’s largest country. (Video: Reuters)

RIO DE JANEIRO — A 63-year-old nationalist renegade rode a wave of voter rage to Brazil’s presidency on Sunday, marking the most dramatic shift to the right in Latin America’s largest country since the end of the Cold War-era military dictatorship.

Jair Bolsonaro, a far-right lawmaker and former army captain, defeated leftist Fernando Haddad in the runoff, receiving about 55 percent of the vote, according to official results with nearly 100 percent of the ballots tallied. His win adds Brazil to a growing list of countries — from the United States to Hungary to the Philippines — where staunch right-wing nationalists have scored victories at the ballot box.

Bolsonaro ran a social-media-centered campaign similar to Donald Trump’s that promised to attack the corruption of political elites and bring an iron fist to fighting crime. He demonized opponents and polarized the nation with his history of denigrating women, gays and minorities.

Who is Jair Bolsonaro, the man likely to be Brazil’s next president?

In a Facebook Live address immediately after his victory, Bolsonaro took aim at his political opponents, including members of the left-wing Workers’ Party, whom he has threatened to jail or exile. He declared that Brazil cannot continue “flirting with socialism, communism, populism and the extremism of the left.”

But in a subsequent speech to the nation, he called for unity, saying, “This country belongs to all of us, Brazilians who were born here, and those who are Brazilian at heart. Brazil is a country of diverse opinions, colors and orientations. The law is for everyone.”

Bolsonaro won a first round of the election earlier this month but failed to avoid a runoff. His challenger, Haddad — a one-term mayor of Sao Paulo, Brazil’s largest city — had depicted the election as a fight to preserve democracy. Bolsonaro has been an outspoken defender of Brazil’s former military dictatorship, lamenting that it did not kill enough dissidents.

The election occurred in a period when faith has collapsed in Brazil’s corruption-stained political class, the economy has floundered and gang killings have surged, leaving the nation feeling rudderless and besieged. Haddad ran largely as a stand-in for Luiz Inácio Lula da Silva, the popular former president, whose reelection bid was upended when he landed in jail this year on corruption charges. Haddad received about 45 percent of the vote.

Brazilians responded by brutally punishing traditional parties in elections in which dozens lost seats in Congress and Bolsonaro allies scored major wins in Brazil’s most populous states. Outside Bolsonaro’s home in Rio de Janeiro on Sunday, his supporters rallied in the green and yellow of the Brazilian flag, pointing their fingers in the shape of a gun — Bolsonaro’s trademark gesture. They shot fireworks on sidewalks and burst into choruses of the national anthem — a song once reserved for the soccer field, but that Bolsonaro made a symbol of his campaign.

“He’s the only one with the courage to do something different,” said Alexandre Maciel, 44, an asset manager at a financial firm, after voting in Sao Paulo.

Dismissed until recently as an unelectable rabble-rouser, Bolsonaro launched his campaign with no significant political allies, a small party machine and a paltry budget. He overcame those challenges with the power of social media, speaking directly to voters through angry all-caps tweets and Facebook Live videos.

His simplistic, get-tough solutions to Brazil’s deep-rooted problems of crime and corruption played well online, and he developed a movement of hardcore followers including pro-gun and evangelical voters that some analysts compared to Trump’s supporters in 2016.

Even as Bolsonaro was sidelined from the campaign in September — when he was stabbed in the abdomen at a rally — his popularity grew. 

Bolsonaro’s win marks a diametrical shift for Brazil, a nation dominated for most of the past decade and half by the leftist Workers’ Party. He has been a passionate defender of the dictatorship that ruled the country from 1964 to 1985. He has proclaimed himself a political outsider, despite serving seven terms in congress. 

He once said a female politician was too ugly to rape and suggested that having a dead son was better than a gay son. Last year, he suggested that some descendants of African slaves were fat and lazy.

How Jair Bolsonaro entranced Brazil’s minorities — while also insulting them

Bolsonaro has moved toward the mainstream in recent months, celebrating Brazil’s “diversity” in tweets.

But he has convinced supporters that he will upend the status quo. To reduce crime, he has advocated that gun laws be relaxed so civilians could fight fire with fire. To spur the economy, indigenous lands and the vast Amazon region should be opened up for development, he has argued. 

“It was obvious in this election that someone who could build a credible narrative of being different was going to do well. Bolsonaro understood that,” said Oliver Stuenkel, professor of international relations at the Getulio Vargas Foundation in Sao Paulo. “He was politically incorrect, a bit weird. But that’s one way he has been able to set himself apart from the rest.”

Bolsonaro at times has appeared to mimic Trump, on whom he has lavished praise. He has promised to make Brazil “great” and picked a war with the media over “fake news.” 

Late Sunday, Bolsonaro said that Trump had called to congratulate him on his victory, wishing him luck in what he called “obviously a very friendly contact.”

“He was trying to look like Trump,” said Marcos Nobre, a Sao Paulo-based political strategist. “His message to the electorate was, ‘If the U.S. elected a Trump, so can Brazil.’ ”

Bolsonaro grew up a nerdy kid in a German Italian family with five siblings in Eldorado, a speck of a town in rural northern Sao Paulo state. At a time when the military was torturing, exiling and killing other members of his generation for opposing its policies, he saw the army as his ticket out. At 18, he was accepted to the army’s prep school and later made it to Brazil’s equivalent of the U.S. Military Academy at West Point. 

Bolsonaro left the military in 1988 to begin his political career. As a congressman, he frightened peers with his violent rhetoric, calling in 1999 for the assassination of the elected president at the time, Fernando Henrique Cardoso.

Brazil shed a military dictatorship. Now it looks again toward iron-fisted rule.

Though it surged only over the past two months, the Bolsonaro phenomenon began to take off two years ago, observers say. His popularity built in urban areas, where backers became voracious consumers of his missives on Twitter and WhatsApp. It spread to ranchers suffering invasions of squatters on rural farms. White men and wealthy voters, eager to turn the page after a decade of left-wing rule, rallied to Bolsonaro’s side. 

His rise caught many off guard. 

A decade ago, Bolsonaro “was like a burlesque spectacle, a clown,” said Rubens Soares, a longtime journalist at Folha de Sao Paulo, one of Brazil’s largest news organizations. Yet, Soares said, he noticed a fundamental shift last year. Supporters at Bolsonaro rallies were adoring in a way he’d never seen before in Brazil.

“They would carry him through the streets,” he said. “You could tell something was happening.” 

Long seen as an economic protectionist, Bolsonaro did an about-face during the campaign, embracing the free market. But he still played to nationalists, vilifying China for “buying up” Brazil. Twice divorced and now married to his third wife, he nonetheless proclaimed himself a supporter of family values, and his opposition to gay rights and legalizing abortion helped him win over evangelicals, a powerful voter base.

But corruption and the Workers’ Party were his primary targets. Since democracy was restored here in 1985, two presidents have been impeached, one has gone to jail, and Brazil’s current leader has been indicted on a charge of corruption, an accusation he denies. One-third of the lower house is under investigation for corruption, largely tied to a sprawling kickback scheme involving some of the country’s largest companies.Bolsonaro’s outsider-cleans-house platform resonated with Brazilians. 

While some Brazilians began to see Bolsonaro as their hero, others seemed to overlook his bombastic statements in a search for meaningful change. 

“If there had been another decent candidate, I wouldn’t have voted for him,” Jose Colares, 51, a dentist in São Paulo, said of Bolsonaro after casting his ballot Sunday for the far-right candidate. “He’s said a lot of garbage, but he’s the lesser of the evils.”

Correction: An earlier version of this report incorrectly identified Alexandre Maciel’s occupation. He is an asset manager at a financial firm, not a manager at an oncology center.

Could the ‘Trump of the Tropics’ really be the next president of Brazil?

Today’s coverage from Post correspondents around the world

Like Washington Post World on Facebook and stay updated on foreign news