A far-right former army captain who expresses nostalgia for Brazil's military dictatorship won 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.

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.

"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."

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 dictatorship era torturers and his derisive comments about women, blacks and gay people.

He 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 it has struggled to stage a comeback with Haddad, who has 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.

Though they come from opposite sides of the political spectrum, both Bolsonaro and Haddad ran campaigns based on nostalgia for a better time. Bolsonaro frequently evoked the country's 1964-1985 military dictatorship amid promises of a return to traditional values and safer, simpler times. In one of his last appeals to voters before Sunday's voting, Bolsonaro tweeted that he would "defend the family and the innocence of children, treat criminals as such and not get involved in corruption schemes."

The Workers' Party, meanwhile, pushed the narrative that a vote for Haddad would be a vote to bring back the boom years that Brazil experienced under the leadership of da Silva, his mentor. On the eve of the election, da Silva tweeted: "Reach back into your memory, remember what my eight years of government were like."

Bolsonaro's poll numbers have increased by about 15 percent since he was stabbed 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 and a diplomatic heavyweight, 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 once 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."