Breaking a nearly two-year-old deadlock, Mexico and the United States have agreed on a natural gas sale that could improve relations between the two countries and open the way for increased Mexican oil exports in the future.

While the amount of gas Mexico will be selling to the United States is small -- only about half a percent of total consumption -- President Carter said yesterday it represented a breakthrough in often-strained relations between the two nations. He also said it is "a significant step toward providing a new energy source for our country."

The agreement was reached yesterday in Mexico, capping six months of difficult and frequently interrupted talks.

Deputy Secretary of State Warren Christopher yesterday told reporters that the United States would pay $3.625 per thousand cubic feet for 300 million cubic feet of natural gas a day.

The price is the highest anywhere in the world for pipeline gas, though Assistant Secretary of State Jules Katz told reporters that because the amount of gas involved is so small, the effect on consumer prices will be "hardly noticeable." Once the gas starts flowing, it will amount to only 8 percent of the United States' total gas imports.

However, the two nations also agreed to tie the price to the quarterly increases in world crude oil prices. And the parties agreed that either may cancel the deliveries on 180 days' notice.

Christopher told reporters at a White House briefing that the gas deal was unrelated to President Jose Lopez Portillo's scheduled state visit next week.

In recent weeks, however, senior administration officials have been saying in private that Carter had asked State and Energy Department negotiators to move aggressively to get a deal before Lopez Portillo's visit.

Earlier this year Carter and former energy secretary James R. Schlesinger drew heavy fire from Congress for not completing the gas pact with Mexico.

In Mexico City yesterday, a high-ranking Mexican official told Washington correspondent Marlise Simons: "We got an agreement because the U.S. suddenly agreed to our final offers. It was as simple as that. We are very pleased."

The price Mexico will receive for its gas is the equivalent of $21-a-barrel crude oil but significantly less than the price of liquefied natural gas, which is more expensive by a dollar or more per thousand cubic feet.

One almost certain outcome of the deal with Mexico is that Canada, which now supplies the United States with significant amounts of gas, will raise its prices to match the Mexican price. Earlier this year Canada was selling its gas to U.S. pipeline companies at $2.16 per thousand cubic feet, but last August the price was raised to $2.80.

U.S. officials have said recently that they expect Ottawa's National Energy Board to boost Canada's prices to $3.30 per thousand cubic feet or more by January.

Christopher and other U.S. negotiators, including Katz, stressed that the United States and Mexico have negotiated "a framework," and that the actual sale will depend upon further details to be negotiated between the pipeline companies and Petroleous Mexicanos (Pemex), the Mexican state oil monopoly.

The agreement will not require the construction of a new pipeline. U.S. officials, however, say that if the amount of deliveries is increased sharply, Mexico will have to increase pipeline capacity.

The consortium of companies that will take delivery of the gas initially signed an agreement in August 1977, but that deal was rejected by Schlesinger because the administration believed the price was too high.

Under the 1977 pricing formula, the gas would now be flowing across the border at nearly $5 per thousand cubic feet.

The consortium is led by Tenneco and Texas Eastern Transmission, and includes Southern Natural Gas, El Paso Natural Gas, Florida Natural Gas and Transcontinental Gas.

There have been seven negotiating sessions on gas since Carter's February visit to Mexico. The United States at first demanded that the price be tied to America's domestic inflation rate, not to world oil prices.

Mexico wanted prices linked to petroleum product prices, such as those for middle distillate (heating) oil.

By Thursday both sides agreed to peg prices to a composite world crude oil price, details of which still have to be worked out by Pemex and the companies.

Another sticking point in the talks was Mexico's desire for an agreement that would allow Pemex to cancel deliveries on short notice.

U.S. officials stress that the gas sale depends upon how long Mexico continues to have a gas surplus. Under the agreement, Mexico is expected to earn about $1 million a day.

Unlike those in most oil exporting countries, Mexico's oil fields contain a high percentage of so-called "associated gas" -- gas that is in effect produced along with oil. Petroleum engineers and the Library of Congress have argued that providing Mexico with an incentive to produce gas would be to U.S. advantage, because it would encourage increased oil production as well.

Mexico exports nearly a million barrels of oil a day, most of which flows to the U.S. market.

Central Intelligence Agency studies say that Mexico could boost its oil exports by as much as four million to six million barrels a day during the 1980s.