When adjusted for inflation, the trade gap narrowed to $43.2 billion, the smallest since January 2010, from $51.9 billion in May.
The smaller so-called real trade deficit in June suggests the government could raise its initial second-quarter gross domestic product growth estimate, published last week.
Trade subtracted 0.8 percentage point from second-quarter GDP growth. The economy expanded at an annual rate of 1.7 percent, stepping up from the first-quarter's 1.1 percent pace.
Belt-tightening in Washington and weaker global demand weighed on the U.S. economy in the first half of the year, but analysts expect activity to regain momentum for the rest of 2013 as the fiscal policy burden eases and growth in Europe picks up.
(Read more: US service sector soars in July, crushing forecast)
The three-month moving average of the trade deficit, which irons out month-to-to month volatility, fell to $39.5 billion in the three months to June from $40.5 billion in the prior period.
In June, imports of goods and services fell 2.5 percent to $225.4 billion. That reflected hefty declines in petroleum imports and industrial supplies and materials, which tumbled to levels last seen in November 2010.
Exports of goods and services increased 2.2 percent to a record $191.2 billion.
Strong export growth helped to lift the economy out of the 2007-09 recession and signs of a pickup after faltering in recent months should buoy expectations of an acceleration in GDP growth in the last six months of this year.
U.S. exports to the 27-nation European Union rose 1.5 percent in June. Exports to the EU in the first half of the year were down 5.5 percent compared to the same period in 2012.
(Read more: Is China starting a new era of corporate crackdown?)
Exports to China, which have been growing more slowly than in recent years, increased 4.5 percent in June.
China has been one of the fastest growing markets for U.S. goods, and exports to that country were up 4.2 percent for the first six months of 2013.
Imports from China fell 2.2 percent, lowering the contentious U.S. trade deficit with China to $26.6 billion from $27.9 billion in May.
Click here for the latest on the markets.