State officials granted reprieves late today to more than 40 gasoline stations that would have had to close Friday under a state law forcing the major oil companies out of the service station business.

The law requires major oil companies and all other out of the state producers and refiners to divest themselves of their stations, either by selling or leasing them to independent dealers or by closing them.

The state was in danger of losing not only the stations but also their normal gasoline allocations, officials said. State officials feared that under Department of Energy regulations, the oil companies could send the gasoline to other states if the stations closed, according to Marvin Bond, spokesman for the state comptroller's office.

Faced with that prospect, Gov. Harry Hughes, Attorney General Stephen Sachs and Comptroller Louis Goldstein met here today and decided that "to protect motorists they would not allow the closings," Bond said after the meeting.

The deadline probably will be extended to Oct. 1, under existing provisions of the divestiture law, Bond said.

Three companies -- Ashland Oil, Cities Service and Crown Central Petroleum, which now operate a total of 35 stations in the state -- already have applied to the comptroller for extensions, Bond said. A fourth company, Amerada Hess, which operates seven stations, has said it will apply today, according to Bond. A company spokesman, however, said he was unaware of any such plan.

About 17 stations involved are in Prince George's and Montgomery counties, according to a comptroller's office tally.

Bond said that during the extension, the state would continue working to get DOE regulations changed so that even if some stations closed, the state would not lose their gasoline allocations.

Though two of the companies, Ashland and Crown, are working either to sell or lease their stations, the fate of the Cities Service (Citgo) and Hess stations appears in doubt.

A Cities Service spokesman said the company "cannot operate other than the way we have been operating" by running the stations itself. The company has sought an extension for its 29 stations so it can again present its case against the law to the legislature, according to Thomas Spann

A hess spokesman refused to comment on the company's plans for its seven Maryland stations.

The Maryland divestiture law, the first of its kind in the nation, was adopted after independent station operators charged that when gasoline was scare, oil companies made more gasoline available to their own stations at the expense of the independents.

The charges, which the oil companies deny, came during the 1979-74 Arab oil embargo, and the law was passed in 1974.

Contending the law was unconstitutional, several companies fought it in the courts, but in June 1978 the U.S. Supreme Court upheld the law.