The nation's foreign trade deficit narrowed dramatically in March for the second consecutive month leaving the United Stated with its smallest red-link trade figure in almost two years, the government reported yesterday.

Commerce Department statistics showed that imports exceeded exports by only $821.3 million last month, down sharply from a $1.3 billion deficit in February and a $3.1 billion gap in January.

The improvement surprised even government economists, who have been predicting for months that the trade deficit would decline significantly this year from last year's record $28.5 billion.

Moreover, the gains came despite a big jump in the value of oil imports, which soared 12.9 percent last month, mainly as a result of higher Middle East oil prices.

The continued improvement in the trade balance appeared to confirm that the long-delayed impact of the 1977-78 dollar decline finally is beginning to take effect, pointing to further sharp gains in coming months.

The decline of the dollar was supposed to help the trade balance because the depreciation makes foreign imports more costly here and makes U.S. exports more attractive overseas.

But the improvement was delayed for reasons analysts cannot explain. The trade deficit narrowed only gradually during most of last year and widened slightly in October. Only in February did the red-ink figure really shrink.

William Cox, the Commerce Department's deputy chief economist, said the United States finally was "seeing the kind of downward trend in the deficit we expected earlier." Cox called the dip "long overdue."

The improvement in March resulted primarily from a sharp increase in exports which jumped 7 percent over the month to a new annual rate of $14.45 billion. The rise followed a 2.9 percent gain in February.

Exports increased in nine of 10 major categories, from food and live animals, crude materials and manufactured materials to machinery and transportation equipment. Only exports of animals fats declined.

Imports rose less robustly, climbing by 3.2 percent to a new annual rate of $15.27 billion. Imports declined by 8.7 percent in February, in part because of the oil shutdown in Iran.

The deficit for March was the smallest red-link trade figure since May 1977, when the nation recorded a trad deficit of $723 million. Last month's red-link figure was the 34th in a row.

The department also reported the U.S. trade deficit with Japan narrowed in March to $286.3 million, from $781.2 million, while America's surplus with Western Europeans nations rose to $1.14 billion, from $1.13 billion before.

The increase in petroleum imports last month stemmed in part from the fact that there were more days in March than there were in February-increasing the total volume of oil purchases.

However, the actual number of barrels the United States imported each day declined over the month to 8.3 million, from 8.5 million in February.

The $821.3 million trade deficit for March was computed on the department's standard basis. On an alternative measure used by most other nations, the deficit was $1.78 billion, compared to $2.21 billion in February. CAPTION: Chart, United States Balance of Trade, The Washington Post