Germany swung to a trade deficit in May as exports fell and imports grew amid high energy costs.

Germany’s trade deficit–the balance of exports and imports of goods–stood at 1 billion euros ($1.04 billion) in calendar and seasonally adjusted terms in May after recording a EUR3.1 billion surplus the previous month, data from German statistics office Destatis showed Monday.

Economists polled by The Wall Street Journal were expecting a surplus of EUR3.0 billion for May.

German exports fell 0.5% on month to EUR125.8 billion in May, while imports increased 2.7% to EUR126.7 billion. Economists expected a 0.9% increase in exports and a 0.2% advance in imports.

Write to Xavier Fontdegloria at xavier.fontdegloria@wsj.com