President Luiz Inacio Lula da Silva of Brazil has narrowly failed to win re-election outright in the first round of the presidential election.
Lula did not win the absolute majority needed to avoid a run-off
Lula needed at least 50% of the vote to avoid a run-off on 29 October, but fell about 1% short, officials say.
He will now go head to head with former Sao Paulo governor Geraldo Alckmin, who took 41.4% of the vote.
Mr Alckmin gained ground late in the campaign, after President Lula's party was accused of dirty tricks.
He emerged from his Sao Paulo apartment to thank voters for their support.
"I'm heading to the second round with a great chance of winning the election," a beaming Mr Alckmin told reporters.
There was no immediate comment from the president - but his aide Tarso Genro told reporters: "We were ready for the first round and we also will be ready for the second round."
The BBC's Steve Kingstone in Sao Paulo says a run-off against Mr Alckmin is a set-back for Lula - as he likes to be called.
Mr Alckmin says he can win the second round battle
With four more weeks of campaigning ahead, he remains the favourite, but suddenly the race looks much more competitive, our correspondent adds.
The president had a comfortable lead in the weeks leading up to the election, but opinion polls showed a dip in support for him amid scandals involving his Workers' Party.
Two weeks ago two men with links to the party were arrested carrying $800,000 dollars in cash, which detectives believe was to have paid for a dossier of corruption allegations against the president's rivals.
The scandal led Lula to sack his campaign manager, but it resurfaced over the weekend with newspapers publishing photographs of the wads of banknotes.
The president denies ever engaging in smear tactics.
On Friday he pulled out of a final TV debate with other candidates, saying they would launch personal attacks on him.
This move looks to have been a huge tactical error, our correspondent says.
Attention will now focus on the electoral court's inquiry into dirty tricks, with the president himself among those under investigation.
It is four years since Lula, the first left-winger to hold the country's highest office in 50 years, was elected in a landslide victory.
His policies, which include raising the minimum wage and broadening state help to the poorest families, have reduced the number of Brazilians living in poverty.
But many commentators argue that his programmes fail to address the structural problems that underpin poverty, such as education.
Mr Alckmin has drawn support from the business community and middle class voters angered by a corruption scandal last year involving government lawmakers.
Brazilians also elected state governors, all congressional representatives and a third of senators, with more than 125 million voters taking part.