Federal prosecutors named the army linguist, arrested in Rhineland state, only as Abdul Hamid S. He is understood to have known details of German military operations in Afghanistan.
Prosecutors said he was suspected of “having passed on his knowledge to an Iranian intelligence service”.
The EU has imposed sanctions on Iran for various alleged spying operations.
This month the EU put a unit of the Iranian intelligence agency and two individuals on the EU terrorist list, freezing their financial assets.
Iran says the accusations were fabricated to damage EU-Iran relations.
The German armed forces (Bundeswehr) have up to 1,300 personnel in the international security force in Afghanistan, with a mandate currently running until the end of March.
Based in Mazar-e-Sharif, a northern city, the German contingent’s job is to train Afghan government troops, carry out surveillance and protect humanitarian missions. They have been involved in clashes with Taliban insurgents.
The Iranian state has been linked to several attacks and plots against Iranian dissidents in Europe, though Tehran rejects this.