MEXICO CITY, July 20 (Reuters) - The U.S. decision to formally challenge Mexico's energy policies brings to a head a dispute that has grown steadily between President Andres Manuel Lopez Obrador and some of his country's top trade allies and investors since he took power in late 2018.

On Wednesday, the U.S. government said it would request dispute settlement talks with Mexico under a North American trade deal over policies championed by Lopez Obrador that Washington argues treat American companies unfairly.

Canada quickly said it would join the U.S. complaint over those policies, which have helped to cloud investor sentiment in Mexico since the government first began trying to rework existing contracts in the sector in 2019.

Lopez Obrador, a leftist energy nationalist, responded defiantly, telling reporters that Mexico was not violating the United States-Mexico-Canada Agreement (USMCA) and that his administration would defend its policies rigorously.

However, Carlos Vejar, a former Mexican trade negotiator, said Lopez Obrador's government would likely struggle to argue its case under the trade pact, which came into effect in 2020 as a replacement for the North American Free Trade Agreement.

"It looks difficult for Mexico to defend all the measures that are being identified," Vejar said.

Lopez Obrador has spent billions of dollars trying to revive state-owned oil company Petroleos Mexicanos (Pemex) and has changed the law to favor national power utility Comision Federal de Electricidad (CFE) over private-sector energy investors.

Business associations argue the policies have led to unjust treatment of private firms like Talos Energy and Spain's Iberdrola, and breach laws liberalizing the energy market passed under the previous Mexican government.

The Mexican president says tweaks he made to USMCA give Mexico the right to put the interests of the state first.

Despite setbacks in lower tribunals that he says back his adversaries, Lopez Obrador has persisted in tightening state control of energy, and in April won a partial victory in Mexico's Supreme Court on measures giving precedence to CFE.

However, the challenge thrown down to Mexico by the office of the U.S. Trade Representative has escalated the dispute and, if unresolved, could lead to punitive U.S. tariffs.

Mexico's economy ministry said it was willing to reach a "mutually satisfactory" solution to the dispute. How it pans out is likely to be closely watched by investors from Europe and other parts of the world also irked by Mexico's policies.

Vejar, the former trade negotiator, said a resolution could be reached within a year if the controversy goes to dispute panels. But a lasting fix would likely depend on whether Lopez Obrador was prepared to negotiate a compromise, he added.

The USTR step was quickly hailed by the private sector in the United States, which is by far Mexico's biggest trade partner and the main source of its foreign direct investment.

Ken Salazar, the U.S. ambassador to Mexico, said in June that Mexico's energy policies had put at risk some $30 billion in existing and planned U.S. investment projects in Mexico.

The U.S. challenge is a setback for Lopez Obrador, who only last week met with his U.S. counterpart Joe Biden in Washington. There, the Mexican president said U.S. companies were primed to invest billions of dollars in his country's energy sector.

Lopez Obrador also announced that Biden had agreed to increase the number of work visas for Mexicans in the United States, although Washington has yet to confirm that.

U.S. allies of Mexico say business disputes with Mexico are not helping them build support for visas among employers.

"(Lopez Obrador) can't go after American businesses and then at the same time say: 'I need more work visas'," said Henry Cuellar, a Democratic congressman from Texas and staunch advocate of free trade between the United States and Mexico.

(Reporting by Dave Graham; Additional reporting by Ted Hesson in Washington; Editing by Paul Simao and Marguerita Choy)