A rare gain in U. S. exports helped to chop the nation's merchandise trade deficit by nearly one-third in the first quarter, the government said yesterday. However, Commerce Secretary Malcolm Baldrige said that record foreign trade deficits still loom.

A big decline in oil imports contributed to the first-quarter improvement in the deficit, but Baldrige said imports of all kinds should be rising again soon, pushing the deficit higher.

In all, the deficit for U. S. foreign trade in manufactured goods, farm produce, oil and other merchandise narrowed to $8.4 billion in the January-March period from $12.1 billion in the last three months of 1982, the Commerce Department said. If the red ink were to continue at the first-quarter level, the deficit for the entire year would be less than last year's record $36.3 billion.

However, Baldrige said that "our outlook for a large increase in the trade deficit for all of 1983 remains unchanged." And he said that department economists also still expect a record yearly deficit of more than $20 billion in the nation's current account, which includes trade in services and various financial transactions as well as the merchandise included in yesterday's report.

The new report runs basically parallel to monthly Commerce figures which had shown the merchandise trade deficit narrowing to $10.8 billion in the first quarter from $12.8 billion in the final quarter of last year. Yesterday's version includes most of the same items but excludes military trade and computes shipping charges in a more favorable way.

The new version said total U.S. exports increased $1.9 billion, or 4 percent, to a total of $50 billion in the first three months of the year, the first quarterly increase in two years.

Helping push overall exports up were increased wheat sales to the Soviet Union and developing countries, rising sales of civilian aircraft and increasing exports of automotive products to Canada, the report said.

Total imports decreased $1.8 billion, or 3 percent, to $58.4 billion. A $5.2 billion drop in oil imports more than accounted for the overall decline.