In an attempt yesterday to stem a flood of money out of the country, Mexico closed its foreign exchange markets, prohibited banks from transferring money out of the country and said all dollar deposits in Mexican banks could only be withdrawn in pesos.

The emergency measures, announced late Thursday, come in the wake of a second major devaluation this year and the imposition last week of a new two-tier exchange rate system. They are likely to be followed by an austerity plan aimed at enticing foreign capital back into the country, analysts said yesterday.

Mexico, which has traditionally spurned exchange controls and allowed residents to hold dollar accounts at local banks, is suffering from a crisis of confidence, one international official commented yesterday.

Two huge devaluations of the peso this year have not been sufficient to stop the flow of money out of the country, and yesterday's emergency measures indicated that Mexico was running out of foreign exchange to pay its import bills and service its $80 billion of foreign debts.

A further weakening in the peso's value against the dollar is likely when the government reopens the exchange markets, a New York analyst said. This, together with any new measures to fight inflation or reduce government spending, could lead to political unrest, some experts warned yesterday.

It is not yet clear when the government will reopen the exchanges, nor whether it will then allow free trading, or will impose new foreign exchange controls. The two-tier system set up last week allows a preferential exchange rate to be used for buying "essential imports" and for repaying principal and interest on foreign debt.

Mexico's financial crisis has been exacerbated by unwillingness of foreign banks to lend more money at a time when the economy is in difficulties and they foresee problems in the loans they have already made. Some analysts said yesterday that while Mexico does have economic problems, including very high inflation and a large balance-of-payments deficit, these are not as bad as the foreign exchange crisis would suggest.

Austerity measures already taken have helped to shrink the payments deficit this year, one New York banker said yesterday. After running a deficit of close to $12 billion last year, Mexico is now expected to be only about $7 billion in the red this year, he said.

Already the largest industrial group in Mexico has had to reschedule its overseas debts. A Southwestern banker yesterday predicted that other, smaller private borrowers would be unable to meet their interest payments in newly expensive dollars, and would be forced to reschedule.

About 90 percent of Mexico's foreign debt is in dollars, and these become harder for Mexicans to repay as the dollar climbs against the peso.

Traditionally, Mexicans and others have been allowed to hold dollar accounts in Mexico. From yesterday, money in these accounts could only be withdrawn in pesos, effectively ending the "Mex-dollar" accounts. The government announced that the exchange rate to be used to translate the dollars would initially be 68.9 pesos to the dollar, the free market rate at the close of business on Thursday, and would change with the changing free market rate.

The preferential rate is expected to steady at about 50 pesos to the dollar, while the free market rate fluctuated last week from 65 pesos to close to 100 pesos at one point.