SAO PAULO — A far-right former army captain who expresses nostalgia for Brazil’s military dictatorship won the first round of its presidential election by a surprisingly large margin Sunday but fell just short of getting enough votes to avoid a second-round runoff against a leftist rival.

Jair Bolsonaro, whose last-minute surge almost gave him an electoral stunner, had 46.7 percent compared to 28.5 percent for former Sao Paulo Mayor Fernando Haddad, Brazil’s Superior Electoral Tribunal said after all the votes were counted. He needed over 50 percent support to win outright.

Polls predicted Bolsonaro would come out in front on Sunday, but he far outperformed expectations, blazing past competitors with more financing, institutional backing of parties and free air time on television. Despite the sizable victory, polls show the two candidates are neck-and-neck for the Oct. 28 runoff, and much could shift in the coming weeks.

Ultimately, Bolsonaro’s strong showing reflects a yearning for the past as much as a sign of the future. The candidate from the tiny Social and Liberal Party made savvy use of Twitter and Facebook to spread his message that only he could end the corruption, crime and economic malaise that has seized Brazil in recent years — and bring back the good old days and traditional values.

“This is a victory for honest people, who want the best for Brazil,” said Bianca Santos, 40-year-old psychologist, who added Bolsonaro would end high crime rates.

Brazil is the largest economy in Latin America and it is a diplomatic heavyweight in the region. Bolsonaro has promised to shake up its foreign policy, including taking a harder line on Venezuela and other leftist regimes and forging closer ties with the United States. He has been short on details, but he has also promised a more open economy.

But the election largely turned on domestic matters, on which Bolsonaro has alienated nearly as many people as he has attracted. Many fear what Brazil will look like if he wins.

Barbara Aires, a transgender woman who unsuccessfully ran for Rio state representative, said Bolsonaro’s first-round victory represented a “step backward” that could lead to “taking back rights and more violence toward the LGBT community.”

The two candidates have painted starkly different visions of the country’s past and future.

Bolsonaro has portrayed a nation in collapse, where drug traffickers and politicians steal with equal impunity, and moral rot has set in. He has advocated loosening gun ownership laws so individuals can fight off criminals, giving police a freer hand to use force and restoring “traditional” Brazilian values — though some take issue with his definition of those values in light of his approving allusions to the 1964-1985 dictatorship and his derisive comments about women, blacks and gay people.

He once told a fellow congresswoman that she was too ugly for him to rape and said that he would not be able to love a gay son.

While those comments have disgusted many, Bolsonaro has capitalized on Brazilians’ deep anger with their traditional political class and “throw the bums out” rage after a massive corruption investigation revealed staggering levels of graft.

Beginning in 2014, prosecutors alleged that Brazil’s government was run like a cartel for years, with billions of dollars in public contracts handed out in exchange for kickbacks and bribes.

Revelations of suitcases of cash, leaked recordings of incriminating exchanges between powerbrokers and the jailing of some of the of the country’s most powerful people, including da Silva, unfolded like a Hollywood script — and then became one: Netflix released a (barely) fictionalized account of the probe this year.

The Workers’ Party was at the center of that investigation, and many have turned to Bolsonaro as a tactic to keep the party out of office.

“I voted against thievery and corruption,” said Mariana Prado, a 54-year-old human resources expert. “I know that everyone promises to end these two things, but I feel Bolsonaro is the only one can help end my anxieties.”

Meanwhile, the party has struggled to stage a comeback with Haddad after da Silva was barred from running. He has said that many of the allegations against Workers’ Party politicians are political persecution and portrayed a country hijacked by an elite that will protect its privileges at all costs and can’t bear to see the lives of poor and working class Brazilians improve.

Haddad has promised to roll back President Michel Temer’s economic reforms that he says eroded workers’ rights, increase investment in social programs and bring back the boom years Brazil experienced under his mentor, da Silva.

Bolsonaro’s poll numbers jumped after he was stabbed during a campaign event on Sept. 6. He was unable to campaign or participate in debates as he underwent surgeries during a three-week hospital stay, but instead brought messages directly to voters via Facebook and Twitter.

“For a front-runner, the best thing to do is commit as few errors as possible,” said Andre Portela from Getulio Vargas Foundation, a leading university and think tank. “Getting stabbed helped Bolsonaro in that. He wasn’t exposed to debate, to people questioning him.”

The campaign to run Latin America’s largest economy, which is a major trade partner for countries in the region, has been unpredictable and tense. Da Silva led initial polls by a wide margin, but was banned from running after a corruption conviction. Bolsonaro’s stabbing forced candidates, and Bolsonaro himself, to shift strategies and recalibrate.

All along, Brazilians have said their faith in leaders and their hopes for the future are waning.

This election was seen as the great hope for ending a turbulent era in which many politicians and business executives were jailed on corruption charges, a president was impeached and removed from office in controversial proceedings, and the region’s largest economy suffered a protracted recession.

Instead, the two front-runners merely reflect the rabid divisions that have opened up in Brazilian politics following former President Dilma Rousseff’s impeachment and the revelations emerging from the “Car Wash” graft probe.

Caught in the middle are Brazilians who dislike both candidates and see them as symbols of a broken system.

“I think we’re going to continue with the same polarization,” if either Haddad or Bolsonaro wins, said Victor Aversa, a 27-year-old massage therapist who voted for center-left candidate Ciro Gomes, who had been polling third. “We’ve been on this path of crazy bipolarity. Haddad and Bolsonaro will both lead populist governments.”