MEXICO CITY (Reuters) - President Enrique Pena Nieto on Wednesday replaced his close ally and finance minister, Luis Videgaray, after the two were heavily criticized for Republican U.S presidential candidate Donald Trump’s controversial visit to Mexico last week.

A somber-looking Pena Nieto told a news conference that Videgaray, who officials said was the architect of Trump’s visit, would make way for former Finance Minister Jose Antonio Meade.

Pena Nieto was widely pilloried for hosting Trump at short notice last Wednesday. The New York businessman has repeatedly vowed to build a border wall to keep out illegal immigrants - which he said Mexico would pay for.

Trump has further infuriated Mexicans by threatening to carry out mass deportations and rewrite trade treaties crucial to their economy, and by referring to some immigrants from the United States’ southern neighbor as rapists or drug runners

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

“The proverbial last straw was the Trump visit,” said Agustin Barrios Gomez, a politician from the opposition leftist Party of the Democratic Revolution, and a driving force behind a draft bill sent to Congress this week to empower the government to retaliate against Trump if he threatens Mexican interests.

“(It) was seen as such a fiasco that he no longer had the credibility necessary to do his immediate job,” he added.

Senior diplomats said Videgaray, 48, had been instrumental in arranging the American’s visit, in which the government had hoped to impress upon Trump the need to moderate his tone and reconsider his more divisive campaign proposals.

But within hours of leaving Mexico, Trump was telling a cheering crowd of supporters in Arizona that Mexico would pay for the border wall “100 percent,” prompting fresh ridicule of Pena Nieto at home.

Instead of teaching Trump a lesson, the visit “legitimized him as a potential head of state,” said Andres Rozental, a former deputy Mexican foreign minister. “It just shows how little thought went into it.”

Trump commented on the resignation during a forum with U.S. veterans on NBC television when he was asked if the United States could take a chance with him as commander-in-chief.

“Well, I think absolutely,” he said. “I think if you saw what happened in Mexico the other day...I let them know where the United States stands. I mean, we’ve been badly hurt by Mexico...And if you look at what happened, look at the aftermath today where the people that arranged the trip in Mexico have been forced out of government. That’s how well we did.”

Slideshow ( 5 images )

PRESIDENTIAL RACE

Videgaray ran Pena Nieto’s election campaign, and was long regarded as the most powerful Cabinet minister, with a huge influence on policy after orchestrating the country’s landmark energy, telecommunications, tax and education reforms.

Both men were embroiled in conflict-of-interest scandals in late 2014, after Pena Nieto’s wife and Videgaray were found to have acquired property from a major government contractor. Pena Nieto ordered an inquiry, which cleared both of any wrongdoing.

The respected new finance minister, Meade, is a friend of Videgaray and had started in the administration as foreign minister before moving to the social development ministry.

Slideshow ( 5 images )

Videgaray’s departure from the Cabinet means Interior Minister Miguel Angel Osorio Chong, on whose watch capo Joaquin “El Chapo” Guzman escaped from prison before being recaptured, is now the most visible potential presidential candidate in 2018 for the ruling Institutional Revolutionary Party, or PRI.

Polls show the main hopefuls for 2018 face a tough battle, with Osorio Chong leading prospective PRI contenders.

Meade, who was also once energy minister, is among those hopefuls. He must now steer next year’s budget through Congress, with the draft plan to be announced on Thursday.

Senator Ernesto Cordero, a former finance minister from the center-right opposition, said he expected a smooth handover to Meade, whose experience in government he underlined.

“He’s been a minister five times, and on those five occasions he’s done a good job,” Cordero said.

The economy has consistently fallen short of government growth forecasts during Videgaray’s tenure, and contracted in the second quarter for the first time in three years.

The new budget will be under close scrutiny after rating agency Standard & Poor’s last month said it could cut Mexico’s credit rating following a marked increase in debt.

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