The United States and Mexico announced today that they are beginning direct government negotiations to establish a price for the sale of excess Mexican natural gas to American energy companies.

Ending a diplomatically difficult visit here, President Carter also announced that he will meet again this summer in the United States with Mexican President Jose Lopez Portillo to continue discussion of other issues that remained unresolved after two days of talks here. [President Carter returned to the White House shortly before 4:30 p.m.]

In effect, the two countries agreed to continue talking about their differences over energy, immigration and trade in a search for what Lopez Portillo called "permanent agreement and arrangements."

U.S. officials said the gas negotiations would begin as soon as possible, but that there was no commitment to have a general agreement for a natural gas sale before the next meeting of the two presidents.

The agreement for the gas negotiations was the most significant development from Carter's visit, which was marked by a series of blunt reminders to the president that Mexico will not allow its oil and gas riches to be exploited by its long dominant neighbor to the north.

Direct government negotiations aimed at establishing prices and other principles for the sale of natural gas are a departure from normal practices and an outgrowth of a development last year that angered Mexican officials and contributed to the chilly tone of Carter's reception here.

Normally, U.S. energy companies deal directly with foreign governments, agreeing on a price for oil and gas imports subject to approval by the federal government. Last year, six American companies agreed to purchase Mexican gas for $2.60 per thousand cubic feet. But Energy Secretary James R. Schlesinger Jr., objecting that the price was too high, blocked the sale -- which would have amounted to about 4 percent of U.S. consumption.

The government negotiations could clear the way for a new agreement between the American firms and Mexico.

In addition to the gas negotiations, U.S. and Mexican officials signed three minor agreements dealing with technology, urban planning and arid land management.

In a joint communique issued after Carter left Mexico City, the two presidents said they had lengthy discussions about illegal Mexican aliens in the United States. While there was not agreement over this issue, they said the two governments would consult more closely over this and the other differences.

Carter announced that he is naming former governor of Florida Reuben Askew to head a congressionally established commission that is studying U.S. immigration policy and is to report in 1980. American officials said Askew will soon meet with Lopez Portillo here to discuss the issue.

Mexico has traditionally viewed the aliens as an American problem, showing little interest in stemming the migration, which eases the heavy unemployment here.

Carter left here early in the afternoon to return to a White House meeting with his top foreign policy advisers on developments elsewhere in the world, chiefly Iran.

Before he left, Carter addressed a joint session of the Mexican Congress in Spanish, striking a conciliatory tone that marked his earlier comments here as well.

Acknowledging that past U.S.-Mexican relations have sometimes been marred by "mistakes and even abuses of power," the president said the United States "welcomes the growing strength of its great southern neighbor."

Carter also explicitly reaffirmed his acceptance of Mexico's policy toward oil and gas production and export:

"The petroleum reserves of Mexico are the national patrimony of the Mexican people, to be developed and used as Mexico sees fit.We respect the imperative that Mexico will produce at a rate suited to your development objectives. As a good customer, we are prepared to pay a fair and just price for the gas and oil you wish to sell."

The United States now purchases about 80 percent of Mexico's oil exports and would clearly like to increase its purchases, particularly since the loss of Iran as a source. Mexico, while hoping to use its oil to spur economic development, fears duction could overwhelm its fragile economy. that too rapid an escalation of oil pro-

At a news conference, Lopez Portillo reiterated that point, saying Mexico had not agreed to any increase in oil production or the amount of natural gas it will make available to the United States.

Carter's visit had some troubled moments, particularly when Lopez Portillo appeared to lecture him about Mexican determination to be treated as an equal. But the visit ended with both presidents in apparently upbeat moods and an attempt by Lopez Portillo to minimize his sometimes sharp words earlier.

"I do not believe that my words can in any way frighten the Americans," he said adding that he had been "frank" because the United States and Mexico are friends.