The stock market recorded its first gain in a week today in a mild upswing led by coal and independent oil and gas issues.

The Dow Jones average of 30 industrials, which had lost 24.86 points over the past four sessions, eked out a 2.86 gain to 818.87.

Advances outnumbered declines by a 5-3 margin in the overall rally on the New York Stock Exchange.

After several days of selling prompted by the political upheaval in Iran, analysts said some investors turned to buying stocks of companies which might benefit from a squeeze on oil supplies from the Middle East.

Thus, in the coal group, Pittston was up 7/8 at 19 1/4 as of the 4 p.m. close in New York, and Eastern Gas & Fuel gained 1 to 17 5/8.

Among North America oil and gas companies, Superior Oil climbed 6 to 315; Apache Corp. was up 1 5/8 at 21 3/8; Reserve Oil & Gas rose 3/4 to 12 3/8, and McMoRan Exploration added 1 to 14 7/8.

McGraw-Hill Inc., which last week rejected a $40-a-share takeover offer from American Express Co. and may become the subject of a proxy fight from dissident shareholders, closed up 3/8 at $29 a share.

McGraw-Hill was the sixth most active stock on the New York Exchange, as 198,600 shares changed hands. Many speculators, who bought McGraw-Hill stock anticipating a lucrative price from American Express, are anxious for a proxy battle. But the two leading dissident shareholders -- Maxwell Geffen and family member Donald C. McGraw Jr. -- are still undecided about whether to try to wrest control of the publishing company from the current board of directors that turned down American Express' overtures.

At the American Stock Exchange, Houston Oil & Minerals led the active list and gained 3/4 to 18 1/4, and Husky Oil picked up 1 1/4 to 38 3/4 in active trading.

Brokers said the general market's rally apparently was restrained by the prospect of unpleasant inflation news Friday when the government makes its monthly report on the trend of producer prices.

In fact, they said a rise in the producer price index at anything less than a two-digit annual rate for January would be a surprise.

Volume on the Big Board slowed to 23.36 million shares from 28.45 million the day before.

The exchange's composite index gained .27 to 54.74.

Standard & Poor's index of 400 industrials rose .58 to 108.81, and S&P's 500-stock composite index climbed .49 to 97.65.

Ticor jumped 8 1/2 to 30 7/8. American General said it made an unsolicited proposal to acquire the company.

Colonial Penn Group dropped 4 to 22 1/2. Organizations representing 12 million retired Americans are restructuring an insurance-purchasing plan they have with the company.

The Amex market value index climbed 1.75 to 158.74.

In the over-the-counter market, the NASDAQ composite index was up .61 at 123.41.