"I confess that I have a legitimate ambition: I want to go down in history as a good president of Mexico," said Lopez Obrador, who won after losses in the previous two elections. "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 eyed his candidacy nervously.
"This new national project will seek to establish an authentic democracy and we do not intend to establish a dictatorship," 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 had them around 22 per cent and 16 per cent, respectively.
"The tendency favours Andres Manuel Lopez Obrador. ... I recognise his triumph," Anaya said in a speech to supporters.
"For the good of Mexico, I wish him the greatest success," Meade said minutes earlier.
Lopez Obrador supporters began wild celebrations in Mexico City, cruising up and down the central Paseo de la Reforma boulevard honking horns to the tune of "Viva Mexico!" and waving Mexican flags from car windows and moon 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 the trills of mariachi music.
Lopez Obrador spoke to the crowd there around midnight, saying to loud applause: "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," 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!
Lopez Obrador said individual and property rights would be guaranteed, promised respect for the autonomy of the Bank of Mexico and said his government will maintain financial and fiscal discipline.
He said contracts obtained under energy reforms passed under President Enrique Pena Nieto will be scrutinised for any corruption or illegality, but otherwise contracts will be honored: "There will be no confiscation or expropriation of assets. ... Eradicating corruption will be the principal mission," he said.
Lopez Obrador also spoke of support for migrants and said the most forgotten and humble people of Mexico will be given preference in his government. He said he will seek a relationship of "friendship and respect" with the United States. And rather than the use of force to fight spiraling violence, he will look to fix root causes such as inequality and poverty.
"Peace and tranquility are the fruits of justice," Lopez Obrador said.
Exit polling by Consulta Mitofsky for the Televisa network forecast gubernatorial wins for allies of 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.
Lopez Obrador, commonly known by his initials, "AMLO," was making his third bid for the country's highest office, having 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 tamp down rising violence.
But after six years under Pena Nieto, that hasn't been the case.
So deep is Mexicans' discontent with the status quo that not only Lopez Obrador but Anaya, Meade and independent candidate Jaime "El Bronco" Rodriguez all claimed to be the ones representing real change.
In the end, 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 favored 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, Lopez Obrador moderated his message somewhat in both substance and style. He said he will 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 Lopez Obrador in the northwestern city of Rosarito, near Tijuana. "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."
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.
Pena Nieto said in a televised message to the nation that he had congratulated 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. 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.
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.
- AP