Far-right candidate Jair Bolsonaro has won a sweeping victory in Brazil's presidential election.
Mr Bolsonaro won 55.2% of the votes cast against 44.8% for Fernando Haddad from the left-wing Workers' Party, the Brazilian electoral authority reported.
Mr Bolsonaro campaigned on a promise to eradicate corruption and to drive down Brazil's high crime levels.
The election campaign has been deeply divisive. Each camp argued that victory for the other could destroy Brazil.
Mr Bolsonaro's victory constitutes a markedly rightwards swing in the largest democracy in Latin America, which was governed by the left-wing Workers' Party for 13 years between 2003 and 2016.
Image captionSupporters of Fernando Haddad were despondent after the results were announced
For the past two years, the country has been led by a conservative, Michel Temer, following the impeachment of President Dilma Rousseff. But Mr Temer has proven deeply unpopular with Brazilians.
With the outgoing president's approval rating at a record low of 2%, voters clamoured for change but they were deeply divided on which way that change should go.
Mr Bolsonaro's convincing 10-percentage-point victory means the vision he laid out to voters of a Brazil where law and order and family values would be made the priority won out.
He said his government would be a "defender of democracy and the constitution".
"This is not the promise of a party, nor the word of a man. It is an oath before God."
Mr Bolsonaro went on to tell his cheering supporters: "The commitment I assumed with the Brazilian people was to create a decent government, committed to the country and the people. And I guarantee you that I will.
"We will change Brazil's destiny together."
Critics of Mr Bolsonaro are worried that the former army captain, who has expressed nostalgia for the years Brazil was under military rule, may curtail citizens' freedoms and undermine Brazil's constitution.
They are also worried about the rights of minorities following homophobic, racist and misogynistic remarks Mr Bolsonaro made during the campaign and before.
Image captionFernando Haddad said he would fight for those who had not voted for Mr Bolsonaro
Fernando Haddad said he had "a responsibility to join the political opposition" against Mr Bolsonaro and promised to "defend the freedoms of those 45 million people" who voted for him.
He also demanded that the rights of those who had not voted for Mr Bolsonaro be respected.
Mr Haddad won in the north-east of Brazil, the heartland of the Workers' Party and the stronghold of former President Luiz Inácio Lula da Silva, whom Mr Haddad replaced on the Workers' Party ticket after Lula was barred from running due to his corruption conviction.
But Mr Bolsonaro won in all other parts of the country, and in some of them by a very large majority, ultimately giving him a sweeping overall victory.
Presidential Election Results
% valid votes, excluding blank and invalid votes
Counted votes: 99.96%Last update: 21h40 (Brasilia time)
Click on the states for more detailed results
Counted votes: 99.96%Last update: 21h40 (Brasilia time)
There was jubilation in Mr Bolsonaro's camp.
A number of his supporters took to the streets outside his home in Rio de Janeiro to let off fireworks and wave Brazilian flags.
Image captionMr Bolsonaro's supporters celebrated outside his home
"Brazil is partying. Brazil's good people are celebrating," said Carmen Flores, a local leader of Mr Bolsonaro's PSL party.
"All these people here are outraged, upset about corruption and crime, and we are with Bolsonaro. The people have spoken. For the first time I feel represented," Andre Luiz Lobo, 38, told AFP news agency.
Meanwhile, Mr Haddad's supporters were in a sombre mood.
"I'm surprised Brazilians would vote for hate, for guns," a tearful Flavia Castelhanos in Sao Paulo told AFP.
There were some reports of scuffles between rival supporters.
Analysis by BBC's Katy Watson in Sao Paulo
Even before the results came out, Mr Bolsonaro's supporters were dancing for joy – so confident they were of victory.
His promises to get tough on rising crime and his claims of being a clean politician in a country – where many of his rivals have been mired in corruption scandals – have won him millions of admirers.
But his win is going to be a blow for millions.
His links to the military and admiration for the former dictatorship concern many Brazilians, as do his sexist, racist and homophobic remarks.
These are uncertain times, with many worried that – with Mr Bolsonaro in power – the gains made in Brazil since the country returned to democracy 30 years ago could be erased.
A number of Latin American leaders congratulated Mr Bolsonaro:
And the US president's spokeswoman, Sarah Sanders, said in a statement: "President Trump called President-elect Bolsonaro of Brazil this evening to congratulate him and the Brazilian people on today's elections. Both expressed a strong commitment to work side by side to improve the lives of the people of the United States and Brazil, and as regional leaders of the Americas."
Meanwhile, Amnesty International said that taking into account his campaign pledges, Mr Bolsonaro's victory could pose a "huge risk" to Brazil's indigenous peoples, LGBT communities, black youth, women, activists and civil society organisations.
He takes over from Mr Temer on 1 January 2019.
One of his flagship campaign promises was to increase security for Brazilian citizens. He has portrayed himself as a hardliner who will restore safety to Brazil's streets.
He has indicated that his government will aim to relax laws restricting the ownership and carrying of guns.
His economic policy plans include proposals to reduce government "waste" and promises to reduce state intervention in the economy.
He has suggested that Brazil could pull out of the 2015 Paris Agreement on climate change, arguing that its requirements compromise Brazil's sovereignty over the Amazon region.
Mr Bolsonaro has also said that he will "cleanse" Brazil of corrupt politicians, a campaign promise which was very popular with Brazilians who say they are tired of the corruption which has seen dozens of high-ranking politicians from the established parties jailed.