Mexican voters unleashed a political earthquake by electing a leftist as president and giving him a broad mandate to overthrow the political establishment and govern for the poor, after widespread public anger about spiralling corruption and violence in the country.
Andres Manuel Lopez Obrador was on course to win with 53 per cent of the vote, according to a quick count by electoral authorities after polling on Sunday - a remarkable margin not seen in the country for many years.
A prominent exit poll predicted that his party allies were poised to score huge wins in the Senate and lower house, possibly absolute majorities in both.
Mr Lopez Obrador, who campaigned on vows to transform Mexico and oust the "mafia of power" ruling the country, made gains on the back of discontent with the governing Institutional Revolution Party, or PRI, of President Enrique Pena Nieto, and had led opinion polls since the beginning of the campaign.
The PRI, which dominated Mexican politics for nearly the entire 20th century and recaptured the presidency in 2012, was set to suffer heavy losses not just for the presidency but in down-ballot races as well.
In brief remarks at a hotel in central Mexico City, Mr Lopez Obrador called for reconciliation after a polarising campaign and promised profound change that respected the law and constitutional order.
"I confess that I have a legitimate ambition: I want to go down in history as a good president of Mexico," said Mr Lopez Obrador. "I desire with all my soul to raise the greatness of our country on high."
The president-in-waiting devoted much of his speech to appealing to citizens of all stripes and seeking to reassure those who have been nervous about his his candidacy.
"This new national project will seek to establish an authentic democracy and we do not intend to establish a dictatorship," Mr Lopez Obrador said. "The changes will be profound, but in accordance with established order."
Conservative Ricardo Anaya of a right-left coalition and the PRI's Jose Antonio Meade acknowledged defeat shortly after polls closed nationwide. The quick count gave them about 22 per cent and 16 per cent of the vote, respectively.
"The tendency favours Andres Manuel Lopez Obrador. I recognise his triumph," Mr Anaya said in a speech to supporters.
"For the good of Mexico, I wish him the greatest success," Mr Meade said.
Mr Lopez Obrador supporters began wild celebrations in Mexico City, cruising up and down the central Paseo de la Reforma boulevard sounding car horns to the tune of "Viva Mexico" and waving Mexican flags from car windows and sun roofs.
Thousands poured into the sprawling main square known as the Zocalo, where the 64-year-old former mayor had called on his backers to rally. Many danced to mariachi music.
Mr Lopez Obrador spoke to the crowd there about midnight. Amid loud applause, he said: "Today one stage has ended and we are going to begin another one — we are going to transform Mexico."
Retired teacher Susana Zuniga beamed and said the country was experiencing a moment similar to the Mexican Revolution a century ago.
"The people are fed up," Ms Zuniga said. "That is what brought us to this."
US President Donald Trump tweeted congratulations: "I look very much forward to working with him. There is much to be done that will benefit both the United States and Mexico!"
Mr Lopez Obrador said individual and property rights would be guaranteed, promised respect for the autonomy of the Bank of Mexico and said his government would maintain financial and fiscal discipline.
He said contracts obtained under energy reforms passed by the Pena Nieto government would be scrutinised for any corruption or illegality, but otherwise would be honoured.
"There will be no confiscation or expropriation of assets. Eradicating corruption will be the principal mission," he said.
Mr Lopez Obrador also spoke of support for migrants and said the most forgotten and humble people of Mexico would be given preference in his government. He said he would seek a relationship of "friendship and respect" with the US. And rather than the use of force to fight spiralling violence, he vowed to address the root causes of violence, such as inequality and poverty.
"Peace and tranquillity are the fruits of justice," Mr Lopez Obrador said.
Leftist ‘AMLO’ favourite as Mexicans choose new leader
Exit polling by Consulta Mitofsky for the Televisa network forecast wins for allies of Mr Lopez Obrador's Morena party in at least four of eight state races on the ballot plus for the head of government in Mexico City. The central state of Guanajuato was expected to go to a candidate of the conservative National Action Party.
Mitofsky predicted Morena allies would take between 56 and 70 seats in the 100-member senate and between 256 and 291 spots in the 500-seat lower house.
Mr Lopez Obrador, commonly known by his initials, Amlo, was making his third bid for the country's highest office after falling short in the 2006 and 2012 elections.
In 2006, rivals convinced voters that his populist message was "a danger for Mexico". In 2012, voters restored the long-dominant PRI to power in hopes that it could cut back-room deals to reduce down rising violence.
But after six years under Mr Pena Nieto, that has not been the case.
So deep is Mexicans' discontent with the status quo that not only Mr Lopez Obrador but Ms Anaya, Mr Meade and independent candidate Jaime "El Bronco" Rodriguez all claimed to be the ones representing real change.
In the end, Mr Lopez Obrador's rants against the "mafia of power" rang most authentic.
"The anger that the average Mexican feels toward the way things are being governed has favoured Lopez Obrador," said Shannon O'Neil, senior fellow for Latin America Studies at the Council on Foreign Relations. "He's been able to capture the mantle of the person who's on the outside who wants change."
After 12 years of near-permanent campaigning, Mr Lopez Obrador moderated his message somewhat in both substance and style. He said he would govern for the poor and fight rampant corruption, and also pulled back from promises to completely scrap energy and education reforms instituted by the Pena Nieto administration.
He also pledged to increase support payments for the elderly and to give scholarships or paid apprenticeships to young people.
"Now that he has won, he cannot fail this new generation that believes in him," said Mariano Bartolini, a 29-year-old lawyer who voted for Mr Lopez Obrador. "It is thanks to us young people who are supporting him that he was able to get more votes than he did in past elections."
Mr Lopez Obrador's rivals argued he could set the country back decades and lead to disaster with an interventionist economic policy.
"I am concerned that some candidates are making proposals that are impossible, because they're very expensive to carry out," said Juan Carlos Limas, 26, an Anaya supporter.
Mr Pena Nieto said in a televised message to the nation that he had congratulated Mr Lopez Obrador and assured him that his administration would work to ensure an orderly transition.
Sunday's elections for posts at every level of government were Mexico's largest ever and became a referendum on corruption, graft and other tricks used to divert taxpayer money to officials' pockets.
Much ire was aimed at the PRI, whose market-oriented economic reforms have yet to benefit many in a country where about half the population is poor. Mr Meade struggled during the campaign to escape discontent with the party, even though he was never a member until it tapped him as an "outsider" candidate.
Mr Anaya tried to harness the youth vote with an emphasis on technology and new ideas, but he divided his own conservative party to take its candidacy and there were doubts his new allies in the leftist Democratic Revolution Party would turn out for someone from the other end of the ideological spectrum.
Sunday's vote was largely peaceful and electoral authorities reported only isolated incidents.