Berlin, Germany (CNN) -- German President Horst Koehler resigned suddenly Monday over what he said was heavy criticism about comments he made on Germany's military role in the world, which he said had been misinterpreted.

It was the first time in German history that a president has resigned, the government-funded Deutsche Welle news agency reported.

Koehler said May 22 upon his return from a trip to Afghanistan that "in emergencies, military intervention is necessary to uphold our interests, like for example free trade routes, for example to prevent regional instabilities which could have a negative impact on our chances in terms of trade, jobs and income."

He later said he meant the protection of shipping routes along the Gulf of Aden against piracy, for example, and not any reference to Germany's role in Afghanistan. But his comments came in for widespread criticism.

Crticis said he was framing Germany's involvement in the Afghan war in economic terms having to do with self-interest, rather than as a duty in fulfilling a NATO mission in the fight against terrorism. Others said his comments indicated he would use the military unconstitutionally and for economic reasons.

Germany has 4,500 troops in a NATO-led force fighting a Taliban-led insurgency in Afghanistan.

Polls show that most Germans oppose the war in Afghanistan, but the government has not set a date for the withdrawal of troops.

Koehler, 67, made the surprise resignation announcement at a televised news conference with his wife standing next to him. He appeared visibly emotional, the Deutsche Welle news outlet reported.

"I am resigning my post as federal president with immediate effect," Koehler said.

"It was an honor for me to serve Germany as president," he said. "I thank the many people in Germany who have put their trust in me and supported my work. I ask for you to understand my decision."

The president's office is largely ceremonial. Chancellor Angela Merkel heads the government and holds the real power.

Merkel found out about the resignation a short while before Koehler's announcement, Deutsche Welle said.

She canceled a trip to northern Italy to visit the training camp of the German national soccer team as the squad prepares for next month's World Cup, the government news agency said.

Speaking at a news conference in Berlin, Merkel said she was stunned by Koehler's decision.

'I deeply regret this resignation," Merkel said. "I must add that I of course told him also I respect his decision to resign."

Merkel also said she had "unsuccessfully tried to get him to change his mind," Deutsche Welle said.

The resignation comes at an inopportune time for Merkel, whose center-right coalition recently lost an important state election and suffered another resignation of a key ally last week, Deutsche Welle said.

The German constitution mandates that the head of the Bundesrat, the upper house of parliament that represents Germany's federal states, will temporarily take over the president's duties. Jens Boehrnsen, the Social Democratic mayor of Bremen, holds the rotating position of Bundesrat leader this year and will assume the president's duties.

Boehrnsen will fill in for Koehler until an electoral college, the Federal Convention, elects a successor. The convention must convene within 30 days -- by June 30 in this case.

The president in Germany is not elected by the people, but by the Federal Convention.

The secret ballot vote is held without prior debate and requires an absolute majority to elect a president. If none of the candidates wins an absolute majority after two rounds of voting, the person with a simple majority in the third round is selected president.

The Federal Convention is Germany's largest parliamentary body. All of the members of the lower house of parliament, the Bundestag, can vote. A corresponding number of representatives from the federal states also are entitled to vote.

The convention, which had 1,224 members at the time, last met in May 2009 to re-elect Koehler.

Koehler, a former managing director of the International Monetary Fund, was first elected president in 2004.