A far-right candidate, Jair Bolsonaro, has won the first round of Brazil's presidential election.
He will face the left-wing Workers' Party candidate, Fernando Haddad, in the second round on 28 October after he failed to win the 50% of valid votes needed to win outright.
With almost all the votes counted, Mr Bolsonaro had 46% and Mr Haddad 29%.
Opinion polls conducted before the election predicted that in a second round the two candidates would be tied.
Mr Bolsonaro's once insignificant Social Liberal Party (PSL) is poised to become the largest force in Congress following legislative elections held alongside the presidential vote, in what analysts have described as a seismic shift in Brazilian politics.
The politician and the PSL have ridden a wave of rising anger at the Workers' Party, which their supporters blame for a prolonged recession, rising violent crime and widespread corruption in South America's largest economy.
In his victory speech, broadcast live on Facebook and uploaded on to Twitter, he said Brazilians could take the path of "prosperity, liberty, family, on God's side" or the path of Venezuela.
Brazil's socialist-led neighbour is mired in a deep economic and political crisis which has driven more than two million people to leave.
Across Latin America this has become a popular campaign strategy: don't vote for the left or you will end up like Venezuela.
The former army captain has made provocative statements on a huge range of issues.
He has pledged tough punishments for offenders and the relaxing of gun ownership. He has also spoken of torture as a legitimate practice and wants to restore the death penalty.
His hard-line approach to law and order has brought back memories of the two-decade military dictatorship, and earned him backing from the military and those demanding greater safety in a country with rising levels of violent crime.
Jair Bolsonaro has strong support within the armed forces
Misogynistic and homophobic rhetoric has prompted outrage and protests, and he has taken a strong anti-abortion stance.
Like Mr Trump, Mr Bolsonaro has criticised the Paris climate agreement, and he has vowed to make Brazil great again.
Economically, he favours a smaller state. He has announced plans to lower taxes, privatise state companies and limit foreign ownership of natural resources.
The candidate believes selling off companies will help fight government corruption - one of the focuses of Operation Car Wash, a massive corruption investigation.
Mr Bolsonaro was stabbed on the campaign trail, drawing intense media attention to him and his policies.
Katy Watson, BBC South America correspondent
Jair Bolsonaro expected to win the presidency in this first round, even if the polls said otherwise. "On the 28th October, we can all go to the beach," he said, as he turned up to vote on Sunday. His supporters had been saying for weeks that their candidate would win this straight out.
Mr Bolsonaro may have soared in the polls recently but Brazilians are going to have to wait another three weeks to find out whether it'll be him or Fernando Haddad as Brazil's new leader.
Brazil feels very divided - and fragile. You could feel it when you talked to voters. So many people have told me they would be voting for the "least worst" candidate. On one side, there are those determined never to allow the Workers' Party to rule again; on the other, those desperately trying not to allow a far-right candidate to rule this young democracy.
A feeling of nervousness hangs over Brazil - and will do for the next few weeks as both candidates ramp up their campaigning once again. The future of Brazil will vary greatly, depending on who eventually wins.
He has won over many Brazilians who think his law-and-order stance will make Brazil safer.
A Roman Catholic, he has also attracted the support of millions of evangelical Christians by saying he will defend traditional family values.
Supporters of Jair Bolsonaro celebrated outside his home in Rio de Janeiro
Several prominent Brazilian footballers have endorsed his candidacy including World Cup winner Ronaldinho, Palmeiras midfielder Felipe Melo and Tottenham Hotspur winger Lucas Moura.
Results showed Mr Bolsonaro dominating Brazil's two biggest states, São Paulo and Rio de Janeiro. However, the country's north-east showed more loyalty to the Workers' Party, having benefited from its social policies.
Centre-left presidential candidate Ciro Gomes got the most support in the state of Ceará, where he has lived for most of his life.
In Sao Paulo, Mr Bolsonaro's son Eduardo was re-elected as a state deputy, receiving more votes than any other lower-house candidate in Brazil's history.
Eduardo Bolsanaro has previously tweeted a photo of himself and former chief strategist to US President Donald Trump, Steve Bannon - who, he says, is "in touch" with his father's campaign to help fight "cultural Marxism".
Mr Bolsonaro said that he was certain that if there had not been "problems" with the electronic voting system used in Brazil, he would have won outright.
Brazil's electoral authorities have said the vote went ahead peacefully and without any major problems.
Fernando Haddad was visibly relieved that there would be a second round
He has portrayed himself as a trusted candidate for those who baulk at Mr Bolsonaro's style and rhetoric.
After reaching the second round, he said he and the Workers' Party would "only use arguments, we don't use any guns".
Referring to Mr Bolsonaro's lead, he said he felt "challenged by the results, which alert us to the risks Brazilian democracy is facing".
"We need to approach this with a sense of responsibility," he told his cheering supporters. "We want to unite the democrats of this country, to reduce inequality and to achieve social justice."
He said he and his party had been presented with "a golden opportunity" by making it into the second round.