MEXICO CITY — Republican presidential nominee Donald Trump and Mexican President Enrique Pena Nieto emerged from their surprise meeting, admitting they discussed the wall.

But the controversial question of who would pay the billions of dollars to build the massive barrier?

You might say they were on opposite sides of the fence.

“We did discus the wall,” Trump told reporters at a joint news conference with the Mexican leader after their meeting. “We didn’t discuss who pays for the wall. That will be for a later date.”

Silent at that moment, Pena Nieto later tweeted, “At the start of the conversation with Donald Trump I made it clear that Mexico will not pay for the wall.”

With the meeting held behind closed doors in Pena Nieto’s official residence, it was impossible to know who was telling the truth. But the difference in how Trump and Pena Nieto recalled their talk was an example of the political risk taken on by two unpopular politicians who arrived at the meeting having spent months quarreling from afar.

On Mexican soil for the first time as the Republican nominee, a firm but measured Trump defended the right of the United States to build a massive border wall along its southern flank, standing up for the centerpiece of his immigration plan in a country where he is widely despised.

Republican presidential nominee Donald Trump delivers a joint press conference with Mexican President Enrique Pena Nieto in Mexico City on August 31, 2016. / AFP PHOTO / YURI CORTEZYURI CORTEZ/AFP/Getty Images

Republican presidential nominee Donald Trump delivers a joint press conference with Mexican President Enrique Pena Nieto in Mexico City on August 31, 2016. / AFP PHOTO / YURI CORTEZYURI CORTEZ/AFP/Getty Images

But within hours of Trump’s visit, the dispute arose over the most contentious part of the billionaire’s plans to secure the U.S. southern border — his insistence that Mexico must pay to build that wall.

Trump began his campaign by deriding Mexico as a source of rapists and criminals, and piled on in the months to come as he attacked Mexico over free trade, illegal immigration and border security. Pena Nieto responded by condemning Trump’s language, saying those were the sort of words that gave rise to Adolf Hitler.

Pena Nieto did not repeat such criticism Wednesday, but acknowledged Trump’s comments had “hurt and affected Mexicans.”

“The Mexicans deserve everyone’s respect,” he said.

The trip and the later dispute, arriving 10 weeks before America’s presidential Election Day, came just hours before Trump was to deliver a highly anticipated speech in Arizona about illegal immigration. That has been a defining issue of his presidential campaign, but also one on which he’s appeared to waver in recent days.

Trump stayed on script after the meeting, reading a statement from notes and politely answering shouted questions from reporters about his promise to force Mexico to pay for a wall along the border between the two countries.

“We did discuss the wall. We didn’t discuss payment of the wall,” Trump said.

Writing later on Twitter, Pena Nieto said the subject was among the first things the men discussed. He has for months said “there is no scenario” under which Mexico would pay for the wall.

“From there, the conversation addressed other issues, and developed in a respectful manner,” he added.

Those issues included the North American Free Trade Agreement, which Trump has called the worst trade deal in history. Pena Nieto suggested there was room to improve the trade deal, while the New York businessman promised to promote trade deals that would keep jobs in the Western Hemisphere — a departure from his standard “America First” rhetoric.

Trump’s presence Wednesday, his first meeting with a head of state abroad as a presidential candidate, sparked anger and protests across Mexico’s capital city. Former Mexican President Vicente Fox bluntly told the celebrity businessman that, despite Pena Nieto’s hospitality, he was not welcome.

“We don’t like him. We don’t want him. We reject his visit,” Fox said on CNN, calling the trip a “political stunt.”

Pena Nieto was less combative as he addressed reporters alongside Trump. He acknowledged the two men had differences and defended the contribution of Mexicans working in the United States, but he described the conversation as “open and constructive.” He and Trump shook hands as the session ended.

Pena Nieto’s performance came in for immediate condemnation from his many critics in Mexico.

“Pena ended up forgiving Trump when he didn’t even ask for an apology,” said Esteban Illades, the editor of Nexos magazine. “The lowest point of the most painful day in the history of the Mexican presidency.”

After saying during his Republican primary campaign he would use a “deportation force” to expel all of the estimated 11 million people living in the United States illegally, Trump suggested last week he could soften that stance.

But he still says he plans to build a huge wall — paid for by Mexico — along the two nations’ border. He is under pressure to clarify just where he stands in the Wednesday night speech, which had been rescheduled several times.

Trump’s running mate, Indiana Gov. Mike Pence, told CBS earlier in Wednesday that Trump would make clear “that there will be no path to legalization, no path to citizenship. People will need to leave the country to be able to obtain legal status or obtain citizenship.”

Campaigning in Ohio, Democrat Hillary Clinton jabbed at Trump’s Mexican appearance as she promoted her own experience working with foreign leaders as the nation’s chief diplomat.

“People have to get to know that they can count on you, that you won’t say one thing one day and something totally different the next,” she told the American Legion in Cincinnati.

Her campaign jumped on Pena Nieto’s later tweet, too.

“It turns out Trump didn’t just choke,” said Clinton campaign chairman John Podesta in a statement, “he got beat in the room and lied about it.”

Christopher Sherman reported from Mexico City, Steve Peoples reported from Washington. AP writers Mark Stevenson and Maria Verza in Mexico City and Jill Colvin in Phoenix and Sun-Times staff contributed to this report.