The closely watched U.S. merchandise trade deficit dropped to $12.2 billion in December as sales of American products overseas reached their highest level ever, but the total deficit for 1987 still reached a record $171.2 billion, the Commerce Department reported yesterday.

The December figures showing the second straight monthly decline were better than most analysts had predicted, and on Wall Street the stock market staged a brief rally and managed to hold its gains.

The Dow Jones industrial average of 30 blue-chip stocks closed up 21.72 points for the day. The report also bolstered the dollar on world currency markets. {Details on Page D11.}

The decrease in the trade deficit to the lowest monthly total since January 1987 also cheered Reagan administration officials, who hailed the start of an export boom and predicted the end of five straight years of record trade deficits.

"We are beginning to see an economy driven by exports, and that means economic vitality and jobs for Americans," said Commerce Secretary C. William Verity, attempting to deflate what had been seen as a prime political issue for Democrats in the November election.

"We hope this is a sign that there are better days ahead," added U.S. Trade Representative Clayton K. Yeutter. "The real trade deficit has turned around, with export industries healthier than they have been in years."

But Rep. Beryl F. Anthony Jr. (D-Ark.), chairman of the Democratic Congressional Campaign Committee, pointed to the $171.2 billion deficit for the year and complained that the executive branch was "basking in the glory of short-term good news and fails to recognize the depths to which this administration has mortgaged the future of this country."

The best news in the December figures appeared to be the strong export performance of American industries, whose $24.8 billion in overseas sales were $1 billion greater than the previous record set in November.

"Clearly, 1987 will be seen as the year that the United States turned the corner in its drive to grow its export industries," said William Lilley III, president of the American Business Conference, an organization of 100 fast-growing midsize companies that trade actively.

Economists and administration officials see continued strong exports as the best way to pull the United States out of its chronic trade deficits and to avoid a recession.

U.S. exports have become more competitive as the value of the dollar has been cut in half over the past three years against other major currencies, especially the Japanese yen and the West German mark.

A cheaper dollar lowers the cost of U.S. products in foreign currencies while increasing the prices of imports in this country.

Another favorable development was that imports held steady at $37 billion in December even though government surveys showed that"This is a major trade reversal, where the trade deficit is going to start to narrow and the dollar is going to strengthen. We've made a major course change." economist Jay Goldinger the costs of foreign products were rising in the United States. This indicates fewer foreign goods are being imported.

"The import volume is down substantially," said William T. Archey, international vice president of the U.S. Chamber of Commerce.

Imports of manufactured goods increased $400 million in December, to $28.8 billion, another sign of how dependent the U.S. economy has become on foreign products.

The import totals showed increases in purchases of foreign office machinery, data processing equipment, airplanes, electrical machinery and clothing and shoes.

Overall, the trade deficit for last year rose almost 10 percent from the 1986 total of $156.2 billion -- a major disappointment for Reagan administration trade officials, who began predicting last spring that the deficit would begin declining in 1987.

Instead, after dipping early in the year, it picked up in the summer and reached a monthly high of $17.6 billion in October before dropping sharply during the last two months of the year.

The continued weak U.S. trade performance was seen by world financial markets as a sign of America's economic decline, and a worse-than-expected $15.7 billion trade deficit for August, reported on Oct. 14, has been widely blamed for triggering the global stock market collapse five days later.

That plunge sent stock prices and the dollar on a downward spiral in markets all over the world.

Now, analysts said, the turnaround has really begun.

"This is a major trade reversal, where the trade deficit is going to start to narrow and the dollar is going to strengthen," said Jay Goldinger, an economist with the Los Angeles bond brokerage firm of Cantor, Fitzgerald. "We've made a major course change. But the U.S. economy is like a big ship, and you don't turn it on a dime."

Imports of manufactured goods rose to $325.2 billion last year from $293.8 billion in 1986, but overseas sales of American-made products also increased -- to $171.5 billion from $148.7 billion.

Exports of U.S. farm products, one of the few areas in which this country maintains a trade surplus, also increased, to $28.6 billion from $26.1 billion.

The United States continued to run its biggest trade deficit with Japan -- $59.8 billion in 1987, up slightly from $58.6 billion in 1986. But the December deficit of $4.8 billion was down $55 million from November.

More significantly, the trade deficit with Western Europe dropped last year to $30.2 billion from $32.7 billion in 1986.

The deficit with Canada also declined, to $11.7 billion from the 1986 total of $13.2 billion.

But the U.S. trade deficit with the newly industrialized countries of Asia increased sharply.

The deficit with Taiwan rose to $19 billion from $15.7 billion; with South Korea to $9.9 billion from $6.4 billion; with Hong Kong to $6.5 billion from $6.4 billion, and with Singapore to $2.3 billion from $1.5 billion.

The U.S. deficit also rose with the OPEC nations, to $14.7 billion from $10.7 billion; with Mexico, $5.9 billion from $5.2 billion, and with Brazil, $4.4 billion from $3.5 billion.