Business confidence in Germany worsened in June as firms in the manufacturing and retail sectors turned more pessimistic due to rising costs and the threat of gas shortages.

The Ifo business-climate index declined to 92.3 points in June from 93.0 points in May, data from the Ifo Institute showed Friday. Economists polled by The Wall Street Journal expected the index to come in at 92.5.

“Sentiment among German managers has clouded over,” Ifo President Clemens Fuest said. German companies were somewhat less satisfied with their current business situation, while their expectations turned markedly more pessimistic, he said.

The index of the current situation fell slightly to 99.3 in June from 99.6 the prior month, while the gauge assessing companies’ expectations fell to 85.8 from 86.9.

“Rising energy prices and the threat of gas shortages are of great concern to German business,” Mr. Fuest said.

The Ifo index is based on a poll of about 9,000 companies in manufacturing, services, trade and construction.

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