As Donald Trump arrived in Phoenix late Wednesday, fresh from a visit to Mexico City’s presidential palace, he had in his hands a big immigration speech that omitted the usual line that Mexico would have to pay for his proposed wall along the U.S. southern border.

Just after landing, though, Mr. Trump discovered that Mexican President Enrique Peña Nieto had tweeted that he had told the Republican presidential nominee during their private meeting earlier that day that his country would refuse to pay for the wall.

Mr. Trump was peeved that Mr. Peña Nieto had gone public with the fact that the Mexican president had broken what Mr. Trump considered a deal to keep the question of paying for the wall off the table at their initial meeting.

So Mr. Trump hurriedly inserted a new sentence in his immigration speech, and he soon boomed out from the podium his traditional declaration that the wall would be paid for by Mexico—adding, “They don’t know it yet but they’re going to pay for the wall.”

“I had no choice,” Mr. Trump said in an interview on Thursday. But he also said of the Mexican president, “I liked him very much.”

All told, Wednesday was a day of exceptional twists and turns, spanning multiple time zones and two countries, on the trade and immigration issues that have become signatures of the Trump presidential campaign.

The day wasn't only one of the most unusual in the campaign, but one of the most controversial. Many Mexicans were incensed that their president invited Mr. Trump. A poll published in the Reforma newspaper Wednesday showed 85% of Mexicans disapproved of the invitation, and 72% thought it weakened the Mexican government.

As the dispute between Mr. Trump and Mr. Peña Nieto over paying for the wall escalated, John Podesta, the chairman of Democrat Hillary Clinton’s presidential campaign, tweeted late Thursday: “What a mess.”

The high-stakes day, which seemed to have been put together at the last minute, actually had been discussed internally for weeks, according to Trump campaign advisers. The campaign saw the meeting in Mexico as a chance to appeal to moderate voters, and the immigration speech as a chance to motivate Mr. Trump’s core voters.

A person close to Mr. Peña Nieto said that “since there was such a clear disagreement in preparatory conversations over issues about the wall and its payment, both parties agreed not to discuss them at the meeting, and instead talk about other topics, such as the great contribution that the Mexican community makes in the U.S., illegal drugs and weapons trafficking, bilateral trade, within North America and the rest of the world.” The person added: “Before the conversation began, Mr. Peña Neto reiterated to Mr. Trump that Mexico won’t pay for any wall, and as agreed, they discussed other topics.”

At the press conference after the meeting, the Trump campaign got the photo op it wanted of Mr. Trump and a world leader. The size of the press turnout pleased the candidate. When asked if they discussed the GOP nominee’s proposed wall, Mr. Trump said they did, but not who would pay for it. Mr. Peña Nieto stayed silent. It wasn’t until landing in Phoenix that Mr. Trump saw the Mexican’s tweet about not paying.

Mr. Trump and his team were stunned, advisers said. News reports were questioning why Mr. Trump didn’t discuss the payment issue at the joint press conference.

Click for more from The Wall Street Journal.