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

But within hours of Trump's visit, a 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.

When answering questions from adjacent lecterns before a Mexican flag after his meeting with President Enrique Pena Nieto, Trump said the two men didn't discuss who would pay for the cost of construction pegged in the billions.

"We did discuss the wall, we didn't discuss payment of the wall. That will be at a later date. This was a very preliminary meeting. It was an excellent meeting," Trump said.

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 Wednesday's meeting held behind closed doors, 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.

10 weeks until election day

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.

Donald Trump has been outspoken about his stance on such issues as illegal immigration, border controls and NAFTA. (Yuri Cortez/AFP/Getty Images)

The trip and the later dispute, arriving 10 weeks before the Nov. 8 election date, came just hours before Trump delivered 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.

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 (NAFTA), 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.

'We reject his visit'

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.

Demonstrators hold placards during a protest against Donald Trump's visit, at the Angel of Independence monument in Mexico City. (Tomas Bravo/Reuters)

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.

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