For generations, Mexico has been widely seen in the United States as a Third World neighbor, a source of cheap labor, illegal immigration and drugs.

But now, Mexico’s growing economic might is transforming relations between the two countries, foreshadowing a new balance of power that was hinted at in President Obama’s visit to the region Thursday and Friday.

Despite growing concerns among U.S. officials that Mexico’s new president, Enrique Peña Nieto, might scale back coordinated efforts to stop the flow of drugs over the border, Obama struck a deferential tone in his appearances here, in marked contrast to a visit four years ago. He said Thursday that it is “obviously up to the Mexican people to determine their security” and on Friday used his remarks to emphasize the United States’ responsibilities toward Mexico.

The new U.S. approach comes at a time of robust growth for the Mexican economy, which has fueled a middle-class expansion. After Canada, Mexico is the second-biggest importer of American-made goods — buying more than Britain, Germany and France or China and Japan combined — and that fact is central to one of the key pillars of Obama’s strategy for growing the beleaguered U.S. economy: exports.

“U.S. exports to Mexico grew $51 billion in the last two years, and that’s more than anywhere else in the world,” said Jodi Bond, the U.S. Chamber of Commerce’s vice president for the Americas. “Mexico is helping us with a manufacturing resurgence, and U.S. companies are growing because the Mexican middle class is growing.”

Mexican pollster and political analyst Roy Campos said that while it is usually Mexico that has the heavier agenda in meetings with the United States, “this time it’s different.”

“It seems like Obama needs more from Mexico than the other way around,” Campos said.

The United States remains enormously important to Mexico, and it still imports from its neighbor significantly more than it exports. But the trade gap is narrowing; it is now the smallest it has been since 2009, when Mexican growth was about to take off.

In his remarks Friday at the Anthropology Museum here, Obama sought to reassure Mexicans that he takes their country’s economic strength and democratic progress seriously.

“Some Americans only see the Mexico depicted in sensational headlines of violence and border crossings. Some Mexicans may think America disrespects Mexico, that we seek to impose ourselves on Mexican sovereignty,” Obama said to cheers. “I have come to Mexico because it is time to put old mind-sets aside. It’s time to recognize new realities, including the impressive progress in today’s Mexico.”

Four years ago, in his first trip as president, Obama came to Mexico and said it was critical to take “urgent and coordinated action” to stem the flow of drugs. He spoke of ramping up law enforcement on the border, providing Mexico with military aircraft and equipment, and ensuring additional high-level­ cooperation with Mexican law enforcement authorities.

But after years of sporadic coordination, the relationship was already changing dramatically when Obama returned this week. Peña Nieto is seeking to limit U.S. involvement in Mexico’s security problems, which include not only drugs but organized crime and kidnappings, and Obama said he had no problem with that new approach.

Rather, he put the focus on what the United States needs to do.

“We understand that the root cause of much of the violence here — and so much suffering for many Mexicans — is the demand for illegal drugs, including in the United States,” Obama said. “We recognize that most of the guns used to commit violence here in Mexico come from the United States.”

U.S. officials said that they anticipate cooperation between the two countries to continue unabated, even as they adjust for the new Mexican president’s expected policy changes.

Ted Piccone, a senior fellow at the Brookings Institution, said Obama’s approach does not represent a “paradigm shift” but reflects domestic politics.

“I think it’s Obama being Obama — respectful and positive when visiting other countries, particularly such an important neighbor like Mexico,” he said by e-mail. “Any president does better in advancing U.S. interests when he plays to both his foreign and domestic audiences. The latter, notably, includes a growing number of Mexican-Americans who vote and whose allegiances to the Democrats helped get him re-elected.

Despite a glum report about Mexico’s economic outlook earlier this week, the country enjoyed growth of 3.9 percent last year. And its economy is still expected to grow 3.5 percent this year, one to 1.5 percentage points faster than the U.S. economy, and pick up speed from there.

Mexico has branched out from its reliance on the United States. When the U.S. economy suffered a terrible economic slowdown, Mexico’s began to charge forward, helped by its growing relationships with Asia.

Carl Meacham, a director of the Americas program at the Center for Strategic and International Studies in Washington, said that, in a change from the past, Mexico and its neighbors have been largely immune to the economic head winds that have confronted the United States.

“They used to say, [if] the United States would catch a cold, the rest of the region would get the flu,” he said. “Right now, it seems that when things on the economic front affect the United States, the region deals with it almost with a shrug.”

“So much of the region is moving to focus on countries around the world,” Meacham said. “China is one of those countries, but they’re also looking at economies in the southeast of Asia — South Korea, Japan — to help buoy economic growth.”

Later Friday, Obama flew to San Jose, Costa Rica, where he was to attend a bilateral meeting, visit with youth and hold a news conference with President Laura Chinchilla.

On Saturday, he will deliver another address and meet with Latin American leaders as part of the regional Central American Integration System — including leaders from Nicaragua, Belize, Panama, Guatemala, El Salvador, Honduras and the Dominican Republic.