Brazil's election is going into a second round in which left-winger Luiz Inácio Lula da Silva will face far-right incumbent Jair Bolsonaro.
With almost all the votes counted, Lula had won 48% against Bolsonaro's 43% - a much closer result than opinion polls had suggested.
But Lula fell short of the more than 50% of valid votes needed to prevent a run-off.
Voters now have four weeks to decide which of the two should lead Brazil.
Winning outright in the first round was always going to be a tall order for any candidate - the last time it happened was 24 years ago.
But President Bolsonaro and Lula had given their supporters hope that they could achieve just that.
Both candidates can and will claim that this as a victory, though.
For Lula - who could not run in the 2018 election because he was in prison after being convicted on corruption charges - this spells a remarkable comeback.
And President Bolsonaro, whom opinion polls had shown trailing far behind Lula, will rejoice in the fact that he proved the pollsters wrong, just as he had predicted he would.
This is a drama which has been years in the making. The two men are arch-rivals and spent much of the campaign trading insults.
In the last TV debate before the vote, President Bolsonaro called Lula a thief, in reference to the corruption charges that put him in jail for 580 days before the conviction was annulled.
Lula, in turn, has labelled Mr Bolsonaro a madman.
Not surprisingly, that tension has filtered down into the streets. During the nights before the vote, neighbours here in Rio could be heard shouting "Lula is a thief" and "Out with Bolsonaro" at each other.
Since the two candidates are such polar opposites, much is at stake.
Lula says he will bolster measures to protect the Amazon rainforest, while Mr Bolsonaro has argued that parts of the rainforest should be opened up to economic exploitation.
Deforestation and forest fires have soared during President Bolsonaro's time in office. Climate activists have warned that if he is re-elected, the area could reach a tipping point.
Critics point out that Lula's environmental record during his time in office - he governed Brazil from 2003 to 2010 - was far from perfect.
But with Mr Bolsonaro counting on the agricultural sector and agribusiness for votes and support, it is Lula who is the preferred choice of climate activists.
But voters in Brazil have many other pressing concerns, such as rising food prices, which have contributed to an increase in poverty and hunger.
Many voters also mentioned education and Brazil's high levels of inequality as issues they want the new president to tackle.
Much of the campaign, however, was overshadowed by concern that Mr Bolsonaro may not accept defeat after he had said that "only God" could remove him from office.
He had also cast doubts on Brazil's electronic voting system, alleging - without providing any evidence - that it was open to fraud.
With the result much more favourable to him than predicted, he is now likely to concentrate more on how to sway those voters who cast their ballot for one of the other nine candidates who were eliminated in the first round.
Lula, who seems to thrive on overcoming obstacles, has already announced that "the fight continues until the final victory, that's our motto".
BBC
Russia's unmanned Luna-25 spacecraft has crashed into the Moon after spinning out of control, officials say.
Donald Trump has confirmed that he will not take part in Republican presidential debates with his rivals in the race for the White House.