The Supreme Court set the stage yesterday for an expected radical restructuring of gasoline retailing throughout the country by upholding a Maryland law that prohibits operation of service stations by major oil companies and all other out-of-state producers and refiners.

Initial reactions created uncertainty as to what effect the decision will have on the prices motorists pay at pumps at Maryland's approximately 3,800 gas stations.

In its 7-to-1 ruling, the court rejected the oil companies' arguments that Maryland was acting unconstitutionally by depriving them of their property without due process of law and by preempting congressional authority to regulate, interstate commerce.

The Maryland law, the first of its kind, was adopted as a response to charges by independent gasoline station operators that when gasoline was in short supply, the oil companies were making more gasoline available to their own stations than to the independents.

The charges - supported by a survey conducted by the Maryland comptroller's office and denied by the oil companies - came during the 1973-74 Arab oil boycott and the resulting gasoline shortage. The law became effective May 31, 1974.

The District of Columbia, Delaware, California and Florida now have similar laws. Thirty-two other states are considering or have considered such legislation.

In addition to its ruling on the question of state regulation of station ownership, the Supreme Court also ruled, 8 to 0, that states can require the oil companies to compensate independent gasoline dealers for any loss they may have suffered as a result of the companies' unequal allocation of gasoline.

Maryland's law requires the oil companies to grant temporary price reductions - called "voluntary allowances" - to independent dealers who had been unfairly denied their fair share of available gasoline.

Justice John Paul Stevens, who wrote the opinion for the court, remarked from the bench yesterday that the decision "does not endorse in any way the wisdom" of Maryland's law.

The dissenting justice, Harry A. Blackmun, denounced it as "protectionist discrimination . . . not justified by any legitimate state interest that cannot be vindicated by more even-handed regulation."

In a friend-of-the-court brief attacking the Maryland law, the Chamber of Commerce of the United States asserted that the issue effected only the petroleum industry, but "the very existence of a dual market structure, in which company-operated retail outlets exist and compete side-by-side with franchised dealers."

In Maryland, at last count, out-of-state producers and refiners operate, with their own employes, 220 stations valued at about $10 million. They now will have to be divested.

Some major oil companies used the stations they owned and operated to test partial self-service and other marketing innovations. Other suppliers sold gasoline under private brand names, such as "Read Head" and "Scot," at consistently lower prices than that charged by the major companies - usually two or three cents a gallon less.

In Anne Arundel Circuit Court, where the suit was originally brought, private-brand marketers argued that they could compete successfully only by retaining control of retail prices, hours of operation, and other aspects of the business.

Anne Arundel Circuit Judge E. Mackall Childs declared the law unconstitutional in January 1976, but was overruled by the Maryland Court of Appeals, the state's highest tribunal, 13 months later. Four major oil companies, led by Exxon Corp., then joined the smaller Ashland Oil Inc., in seeking Supreme Court review.

There was conflicting reaction yesterday on what the law will do to consumer prices. Two oil companies predicted higher prices, while others maintained it was too early to tell, and one association of independent dealers said it would not affect prices at all.

The legislation "may tend to raise gasoline prices in the retail market" because it eliminates supplier-run stations and demands uniform statewide application of the voluntary price reductions previously used only in competitive situations, according to a spokesman for Exxon.

This latter provision would discourage supplies from cutting prices to meet competition in local areas because they would then have to cut prices throughout the state, the spokesman said. Exxon operates 46 of its own stations in Maryland.

The ultimate effect on consumers will depend on what happens to the stations that the oil companies or refiners must sell or lease, according to a spokesman for Gulf Oil Corp.

If independent dealers acquire the stations and run them, as many have been previously run, as self-serve or gas-and-go stations, consumers will enjoy the savings that these stations can provide, the spokesman said. The nationwide average for these savings in 4.5 cents a gallon, he said.

If the stations are all turned into full service operations, consumers will lose the savings they have enjoyed, he added.

In the opinion for the court, Justice Stevens noted arguments that the divestiture law will frustrate rather than enhance competition, as well as warnings by three of the private-brand marketers that if the law is enforced, they may elect to withdraw altogether from the Maryland market.

But, he wrote, evaluations of the economic wisdom of the law must yield to the state's authority to enact it because of the "reasonable relation" it bears to a legitimate state goal: "controlling the gasoline retail market."

If some marketers leave Maryland, "no reason" exists to assume that other interstate refiners will not promptly replace their share of the entire supply, Stevens said. In any event, nothing in the record warrants a conclusion that the law impermissibly burdens commerce, he said.

The out-of-state refiners contended that because the market for petroleum products is nationwide, no state has the power to regulate retail gasoline marketing. Stevens rejected this "novel suggestion."