A late-night count from electoral authorities forecast that leftist candidate Andres Manuel Lopez Obrador would win Mexico's presidential election on Sunday night.

Both of his largest rivals have conceded defeat, and US President Donald Trump has sent his congratulations.

Sunday's elections for posts at every level of government were Mexico's largest ever.

MEXICO CITY (AP) — Furious at spiraling corruption and violence, Mexican voters unleashed a political earthquake Sunday by electing a leftist firebrand as president and giving him a broad mandate to overthrow the political establishment and govern for the poor.

A late-night official quick count from electoral authorities forecast that Andres Manuel Lopez Obrador would win with between 53 percent and 53.8 percent of the vote, 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.

Lopez Obrador, who campaigned on vows to transform Mexico and oust the “mafia of power” ruling the country, rode widespread voter anger and 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, Lopez Obrador called for reconciliation after a polarizing campaign and promised profound change that respects 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 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 percent and 16 percent, respectively.

“The tendency favors Andres Manuel Lopez Obrador. ... I recognize his triumph,” Anaya said in a speech to supporters.

“For the good of Mexico, I wish him the greatest success,” Meade said minutes earlier.

US President Donald Trump also tweeted his congratulations to Lopez Obrador on Sunday evening.

"Congratulations to Andres Manuel Lopez Obrador on becoming the next President of Mexico. 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 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 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.

Voters wanted change

This was the third bid for the presidency by Lopez Obrador. Better known as AMLO, he has said he will rule Mexico for the poor and fight rampant corruption.

"There is a lot of inequality, a lot of violence in this country," said Lopez Obrador voter Hugo Carlos, 73. "This situation has to be changed."

But it was 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.”

Lopez Obrador, who cast his ballot early Sunday, has pledged to give scholarships or paid apprenticeships to youth, and increase support payments for the elderly.

“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.

All the candidates are lambasting President Donald Trump's policies against migrants and Mexico, but voters were wondering who could best deal with Trump.

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.

Lopez Obrador ran on a platform to tackle corruption

Mexico fans celebrate at the Cuauhtemoc monument, with a billboard (TOP L) advertising presidential candidate Andres Manuel Lopez Obrador, after Mexico's 2-1 victory over South Korea during the World Cup on June 23, 2018 in Tijuana, Mexico. Mario Tama/Getty Images

Sunday's elections for posts at every level of government are Mexico's largest ever and became a referendum on corruption, graft and other tricks used to divert taxpayer money to officials' pockets.

Some see this election as Lopez Obrador's best shot at the presidency after 12 years of near-permanent campaigning with his anti-establishment message falling on receptive ears.

"The corrupt regime is coming to its end," Lopez Obrador said at his final campaign event Wednesday. "We represent modernity forged from below."

Much of the popular ire has been aimed at unpopular President Enrique Pena Nieto's Institutional Revolutionary Party, or PRI, whose market-oriented economic reforms have yet to benefit many Mexicans, and Meade struggled during the campaign to escape popular rejection of the party.

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 it's unclear if his new allies in the leftist Democratic Revolution Party will actually turn out for someone from the other end of the ideological spectrum.

Sunday was the first time that an independent candidate appears on the ballot.

Jaime "El Bronco" Rodriguez fought for attention with a horse-mounted "everyman" campaign and by tossing out policy bombs like his proposal to cut off the hands of public officials caught stealing. Without the big party machinery it was an uphill battle.

It is also the first time Mexicans living abroad can vote for down ballot races like senators. More than 181,000 received ballots and the 97,000 that the National Electoral Institute had gotten back by Friday morning were already double what they got in 2012.

Casting his own vote, Pena Nieto promised to work with whoever wins on the transition.

"The president of the republic and his government will be absolutely respectful and support the authorities that are elected," Pena Nieto told reporters.

In both of Lopez Obrador's previous two presidential losses he alleged fraud. In his first loss — by a mere 0.56 percent to conservative Felipe Calderon in 2006 — his supporters held months-long protests in Mexico City and he referred to himself as "the legitimate president."

His allies warned even before Sunday's presidential vote that there better not be any funny business.

Still, the voting was by and large peaceful, apart from the usual complaints about some volunteer-staffed polling places opening late.

The head of the country's electoral institute, Lorenzo Cordova, said voting was proceeding "peacefully, without major incidents," and that only four of the 156,807 polling places failed to open.

On Saturday, the Democratic Revolution party said four of its members were killed in Mexico State, west of Mexico City, while trying to prevent hand-outs of goods by the ruling party to potential voters.

The government of the northern state of Chihuahua reported that three people were arrested in the border city of Ciudad Juarez for allegedly trying to buy votes for an unnamed political party.

And in Michoacan state, polling places in a few villages were cancelled after some inhabitants said they didn't want elections involving political parties, which they mistrust. Some ballots were stolen and burned in the town of Nahuatzen to prevent voting.

In the northwestern border city of Tijuana, 29-year-old engineer Jorge Serrano Martinez said he voted for Lopez Obrador because he wanted "to give the opportunity to another political party to do different things."

"He is not linked to any act of corruption and he has a history as a social fighter," Serrano said. "I think he is the least corrupt of all."

(Associated Press writers Maria Verza and Andrea Rodriguez in Mexico City and Nancy Moya in Tijuana contributed to this report.)