American Telephone & Telegraph Co. yesterday reported a profit in the fourth quarter and sharply higher earnings for 1987 as the company continued to benefit from a major cost-cutting program begun in the previous year.

Chairman James E. Olson said the 1987 results were the "best since divestiture" of the New York-based company four years ago. They were in line with expectations, although some analysts warned that AT&T could face rough times this year as it fights to maintain market share.

AT&T earned $498 million, or 46 cents per share, for the three months ended Dec. 31, compared with a loss of $1.17 billion for the same quarter in 1986. Revenue rose to $8.6 billion from $8.5 billion.

For 1987, the company earned $2.04 billion ($1.88 a share) on revenue of $33.6 billion. That compared with earnings of $139 million (5 cents) on revenue of $34.08 billion in 1986.

Olson attributed much of the earnings increase to a major restructuring program begun in late 1986 and to an increase in long-distance revenue.

In 1986, AT&T said it would cut 32,000 jobs, consolidate facilities and write down the book value of plants, equipment and inventory. The company took a one-time charge of $1.7 billion in the fourth quarter to finance those restructuring plans.

Audrey Stevoff, a vice president and analyst at Duff & Phelps Inc., said AT&T also benefited from a lower tax rate because of changes in the tax law.

"The real question is what happens from now on," said Daniel F. Zinsser, a vice president and telecommunications analyst at Goldman Sachs & Co. "We don't think there's that much more cost-cutting left to do. They now need more revenue."

Increasing revenue may be tough, Zinsser said, partly because more markets that AT&T exclusively served before its divestiture have just started to open up to competitors. Among them are toll-free telephone operations, foreign service and operator assistance.

"AT&T had 100 percent of certain parts of those markets," he said. "But we believe ... particularly in the long-distance business they'll lose market share."

At the same time, AT&T continues to have a difficult time boosting sales of rental products, especially when an increasing number of customers are buying their telephone products.

Stevoff said that despite increased competition from companies such as MCI Communications Corp. and US Sprint Communications Co., AT&T should continue to do well this year.

Particularly encouraging, she said, was the fact that AT&T's product sales continued to increase last year and that the company has reorganized its marketing staff.