Mexican President Enrique Peña Nieto delivers a speech in Mexico City on Dec. 20, 2016. (Edgard Garrido/Reuters)

President Enrique Peña Nieto moved Wednesday to bolster relations with the incoming Trump administration, naming a new foreign minister, Luis Videgaray, who had cultivated ties with the Trump campaign before the election and who has economic expertise at a time when trade issues are coming to the fore.

The appointment is a stunning reversal of fortune for Videgaray, who was ousted as finance minister in the wake of Trump’s visit to Mexico in August amid an outpouring of popular protest.

Videgaray had served as a ­behind-the-scenes liaison to the Trump campaign and advocated for the Republican candidate to visit Mexico as a way to calm financial markets unnerved by the possibility that Trump would win the election and introduce much more restrictive trade and immigration policies.

The visit outraged many Mexicans, who believed that Peña Nieto was bowing down to a candidate who had been harshly critical of Mexicans — referring to immigrants as criminals and rapists and calling for a border wall and mass deportations. But although Videgaray lost his job as finance minister, he remained close to Peña Nieto.

His appointment as foreign minister, announced at the presidential palace, essentially rewards Videgaray for guessing correctly on Trump’s election. It also recognizes the importance of his relationship with Trump’s transition team and his economic expertise at a time when trade ties between Mexico and the United States are being tested.

Trump has threatened to scrap the North American Free Trade Agreement, which has guided commercial relations between the two countries for more than two decades, and he has urged U.S. companies to bring manufacturing jobs home from Mexico.

After the announcement, Videgaray told the Foreign Ministry staff that Mexico needed them more than ever. “The challenge is enormous, the threats are there. But the opportunities and our strengths are also enormous,” he said.

Videgaray is widely considered one of the country’s top economists. He earned a PhD in economics from MIT and spent time as an investment banker before going to work for Peña Nieto when the politician was governor of the state of Mexico, one of the country’s 31 states. Videgaray went on to run Peña Nieto’s presidential campaign and spearhead the administration’s economic reforms.

Peña Nieto said that Videgaray’s experience leading the Finance Ministry, as well as his work with the G-20 countries and with Vice President Biden on bilateral economic issues, “are a fundamental base for his new mission.”

The president said he has instructed Videgaray, before Trump takes office, to “accelerate the dialogue and the contacts so that from the first day of the new administration we can establish the basis for a constructive working relationship. It should be a relationship that permits us to strengthen the bilateral ties in terms of security, migration, commerce and investment. These objectives should be reached at all times promoting the interests of Mexico and without hurting our sovereignty and the dignity of Mexicans.”

Since the election, political observers in Mexico have been predicting Videgaray’s return to the top ranks of government.

“Now he will be in charge of smoothing relations with the new President Trump, because he already has a line of communication with his team,” said Francisco Gil Villegas, a political analyst and professor at the College of Mexico. “It’s a good decision, but it would have been better if he were ambassador — he would have greater influence, greater possibility of lobbying in Washington. But without a doubt, it’s the second-best decision.”

Some analysts were more cautious, saying that the appointment will send the signal domestically that Trump will suffer no political cost for bashing Mexico and that Peña Nieto looks weak and beholden to the new U.S. president.

Peña Nieto, who belongs to Mexico’s historically dominant Institutional Revolutionary Party, has been a deeply unpopular leader, and his efforts to deal with a potential Trump presidency have driven his ratings even lower.

“The problem is not the man, it’s the strategy in general,” said Juan Carlos Moreno-Brid, an economist at the National Autonomous University of Mexico. He cited rising inflation, tepid economic growth and severe inequality as areas that need more attention from the president. “The problem here is the strategy at the heart of the country, the lack of development that has come with this government.”

Peña Nieto said he had accepted the resignation of Foreign Minister Claudia Ruiz Massieu, who had been opposed to Trump’s ­August visit. Peña Nieto also appointed a new culture minister, Maria Cristina Garcia Cepeda, who has held other cultural posts.

Gabriela Martinez contributed to this report.