By Don Nico Forbes

Industrial production in Germany unexpectedly rose for a second consecutive month in May despite higher energy costs and operational uncertainty due to the conflict in the Middle East.

Output was 0.9% higher on month, following an uptick of 0.2% in April, Germany's statistics agency Destatis said Tuesday.

A consensus of economists polled by The Wall Street Journal expected a decline of 0.2%.

The rise in output follows an uptick in factory orders in May, which could point to a recovery in activity ahead. From June, production is also expected to have been supported by easing tensions between the U.S. and Iran, with declining oil and gas costs providing some relief to Germany's energy-intensive industrial sector.

But the Middle East conflict is continuing to make German industrial data difficult to interpret, Sebastian Wanke, an economist at KfW Research, said in a note.

"The effect of bringing forward purchases due to fears of supply shortages is countered by caution stemming from concerns about the future," he said.

Germany's industrial sector had been expected to stage a modest recovery this year after years of subdued output that began before the pandemic. Optimism was fueled by the government's pledge last year to mobilize more than $1 trillion for defense and infrastructure spending.

But the war in Iran has added downside risks to growth. The outlook for industry could also be constrained by higher borrowing costs after the ECB raised interest rates in June to prevent higher energy prices from feeding through into broader inflation.

Still, many economists expect the central bank's focus to shift from inflation risks to slowing growth in the coming months. A sustained recovery in German industry is seen as crucial to lifting the wider eurozone economy.

The ECB lowered its growth forecasts last month, projecting the eurozone economy will expand 0.8% this year and 1.2% in 2027.

But signs of a recovery in German industry are nevertheless gathering steam, Wanke said.

"The recovery has already begun behind the scenes. This is evident in the manufacturing sector's order intake ... This will soon fuel production in this country over the longer term, especially once oil and gas can again pass freely through the Strait of Hormuz and reforms provide additional momentum," he said.

Write to Don Nico Forbes at don.forbes@wsj.com