Mexico's President Enrique Pena Nieto on Wednesday dumped his close ally and finance minister Luis Videgaray after the two were heavily criticized for Donald Trump's controversial visit last week.

A sombre-looking Pena Nieto told a news conference that Videgaray, who officials said was the architect of the visit by the Republican candidate for the Nov. 8 U.S. presidential election, would be replaced by former finance minister Jose Antonio Meade.

Pena Nieto was pilloried on social media for hosting Trump at short notice last Wednesday after the New York businessman had underpinned much of his campaign on building a border wall that he said Mexico would pay for to stem illegal immigration. Trump has also threatened to carry out mass deportations and rewrite trade treaties crucial to the Mexican economy.

Trump insulted Mexicans by saying the country sent rapists and drug runners into the United States.

With economic growth sluggish, the president's popularity at record lows and tensions palpable between the finance minister and other cabinet members, rumours of Videgaray's impending departure have bubbled under the surface in Mexico for months.

The fiery reaction to Trump's visit increased pressure on Pena Nieto to make changes.

Meade, the new finance minister, is a friend of Videgaray's and had served in the administration as foreign minister before moving to the social development ministry last year.

Senior diplomats said Videgaray had been instrumental in arranging the Trump visit. The 48-year-old, who ran Pena Nieto's election campaign, was widely seen as the president's top aide, with a huge influence on policy.

The government had hoped to impress upon Trump the need to moderate his tone, reconsider his more divisive campaign proposals and better understand Mexico's concerns during the 70-minute talk he held with Pena Nieto in Mexico City.

But within hours of leaving Mexico, Trump was telling a cheering crowd of supporters in Phoenix, Ariz., that Mexico would pay for the border wall "100 per cent," prompting fresh ridicule of Pena Nieto at home.

Videgaray, who has also been rumoured as a potential 2017 gubernatorial candidate for the ruling party in the state of Mexico, a populous region next to the capital, will not take another public post, a finance ministry spokeswoman said.

He is exiting shortly before the government presents its 2017 budget with cuts needed to restore confidence after rating agency Standard & Poor's last month said it could cut Mexico's credit rating following a marked increase in debt. The economy has consistently underperformed expectations during Videgaray's tenure.

The economy contracted in the second quarter for the first time in three years, and the collapse in global oil prices has sabotaged Pena Nieto's hopes that landmark energy reform would spur a wave of foreign investment.