WASHINGTON—The U.S. trade deficit narrowed in May as exports rose to their highest level in more than two years.

The foreign-trade gap in goods and services narrowed 2.3% from the prior month to a seasonally adjusted $46.51 billion in May, the Commerce Department said Thursday. Economists surveyed by The Wall Street Journal had expected a smaller trade deficit of $46.2 billion for the month.

Imports...