Mexican banks were nationalized and strict exchange controls imposed today for the first time in the country's history as President Jose Lopez Portillo sought to stem a growing economic crisis that is rapidly assuming complex internal and international political ramifications.

In an emotional speech before the Mexican Congress -- the president's final state of the union address of his six-year term due to end in December -- Lopez Portillo placed the primary blame for Mexico's near-bankruptcy on speculators and capital flight encouraged, he said, by private banks.

"We cannot, with dignity, do anything else. We cannot stand with our arms crossed while they tear out our entrails," he said, outlining the need to stop a massive outflow of money, most of it destined for accounts and investments in the United States.

All banks will be closed until Monday, while the new measures are carried out, Lopez Portillo said. He said that the banks' owners would be compensated and that no foreign banks would be nationalized.

The Mexican announcement was a surprise to U.S. officials and bankers, some of them closely involved in recent international efforts to put together a financial package to bail out the near-bankrupt country.

Under the new exchange controls, all currency transactions will have to be authorized by the government. Mexicans will be required to file applications before taking pesos out of the country, and foreigners entering Mexico will have to declare how much money they are carrying with them when they arrive.

Other details were not available about the controls, which Lopez Portillo admitted would be difficult to enforce. "It is desirable that the controls are transitory," he added.

As has often happened in the past when Mexico confronts its economically one-sided relations with its neighbor to the north, Lopez Portillo took pains to underline Mexico's fierce nationalism and independence.

The Mexican president vehemently reaffirmed his differences with the Reagan administration over policy toward Central America. In some of the toughest language he has used to date, he called on the Reagan administration to leave Nicaragua alone, to stop U.S. attempts to isolate Cuba and to negotiate a solution in El Salvador.

Contrasting his government's attitude toward the region with that of Washington, Lopez Portillo said, "We treat as we would wish to be treated. That is our moral authority in the face of arrogance."

Good relations with Washington are "the cornerstone" of Mexico's foreign relations, Lopez Portillo said, adding, "This is a question of reality, not of tastes or whims."

"In overall terms," he said, these relations are "positive," even though commercially "the United States wants to treat us as if we were an underdeveloped economy without any chance to argue back."

Much of his speech was devoted to his administration's positive accomplishments, ranging from birth control programs to greater employment to an increasingly influential role in world politics.

His insistence on pushing national economic growth to record levels to help assure social peace is viewed by many economists as the source of the current economic problems.

"We haven't sinned, neither as a government nor as a country, and we do not have any reason to make acts of contrition," Lopez Portillo said at one point.

On the increasingly sensitive issue of official graft he insisted, "I have fought corruption to the point of scandal. I don't regret it. The current catharsis is the result."

Lopez Portillo insisted in the 3 1/2-hour speech that he was not conducting a witch hunt in seeking to address economic problems. "We are attempting to correct a great evil. This is not an effort to identify villains."

Just before announcing the nationalization, Lopez Portillo charged that "a group of Mexicans . . . headed, counseled and aided by the private banks, has taken more money out of the country than the empires that have exploited us since the beginning of our history."

Lopez Portillo placed his nation's foreign debt at $76 billion, while some independent estimates are as high as $85 billion. He said Mexicans have $14 billion or more deposited in foreign bank accounts and estimated the value of U.S. real estate owned by Mexicans at $30 billion. Billions more dollars, he said, are going to down payments and mortgages.

He said "all efforts" would be made to keep dollars from leaving Mexico to pay for foreign real estate.

Another $12 billion is tied up in special dollar accounts here in Mexico, he said. Altogether, Lopez Portillo estimated that these account for $54 billion, or two thirds of the country's crushing debt.

Although he conceded that inefficient and wasteful public spending, excessive external debt, overdependence on the oil industry, a mistaken economic policy, and erratic corrective measures might be part of the problem, he said the fundamental causes of the crisis lie elsewhere.

While admitting that there is little his administration can do to make Mexicans bring back the dollars they have sent to the United States, Lopez Portillo suggested that a meeting between the Mexican and U.S. congresses might be "advisable," noting that the problem of capital flight is "a grave, a much more grave problem for us than the drug traffic is for them," a reference to cooperative narcotics enforcement programs Mexico conducts with the United States.

Then, with regard to illegal Mexican immigrants in the United States, Lopez Portillo asked, "How are we going to keep from exporting laborers if the capital that could give them work here is over there?"

In an obvious reference to the Reagan administration's measures against Nicaragua's Sandinista government, Lopez Portillo said, "Nicaragua ought to resolve its own problems by itself. Don't weigh them down with more economic pressures. Don't threaten them with artificial armed interventions."

Lopez Portillo also dismissed U.S. efforts to help the Salvadoran government, with military aid and elections, to win its civil war.