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.
Why is Bolsonaro so controversial?
The former army captain has made provocative statements on a huge range of issues.
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.
Misogynistic and homophobic rhetoric has prompted outrage and protests, while his anti-abortion stance has won him support from millions of evangelical Christians.
His son, Eduardo, 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”.
Economically, Mr Bolsonaro 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 what are seen by many as his divisive policies.
The ‘least worst’ candidate
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.
How did the election pass off?
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.
“I am certain that if this hadn’t happened, we would have known the name of the president of the republic tonight.”
He did not specify what he thought those “problems” were.
Brazil’s electoral authorities have said the vote went ahead peacefully and without any major problems.
What are Bolsonaro’s policies?
Brazilians will have to choose between two very different candidates on 28 October.
Mr Bolsonaro, a Roman Catholic, won the support of many evangelical Christians by saying he would defend traditional family values. He has also won over many Brazilians who think his law-and-order stance will make Brazil safer.
On the eve of Sunday’s vote, he said that his government would hand down the tough punishments offenders deserved. He is also in favour of relaxing gun ownership laws and has spoken of torture as a legitimate practice. He also wants to restore the death penalty.
In his victory speech, broadcast live on Facebook and uploaded on to Twitter, he said there were two paths Brazilians could follow.
“[There is] the path of prosperity, liberty. family, on God’s side… and the other one is that of Venezuela,” he said referring to Brazil’s socialist-led neighbour, a country mired in a deep economic and political crisis which has driven more than two million people to leave.
“We can’t take another step to the left!” he urged voters. “We can’t go fraternising with socialism or communism,” he said.
He acknowledged that a lot of criticism had been levelled against him but vowed to “unite the [Brazilian] people”.
“Together we will be a great nation,” he said.
What does Haddad advocate?
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.