* FSB security service detains ex-journalist Ivan Safronov
* Safronov is now aide to head of Russian space agency
* FSB says he passed military secrets to a NATO country
* Safronov denies treason charge - TASS (Adds details)
By Andrew Osborn and Tom Balmforth
MOSCOW, July 7 (Reuters) - Russian security forces on Tuesday detained a former journalist who works as an aide to the head of Russia's space agency and accused him of treason, saying he had passed military secrets to an unnamed NATO power.
Footage released by the FSB security service showed Ivan Safronov being detained outside his Moscow flat by armed agents who searched him before putting him into a van.
He could later be heard saying "I'm not guilty" as masked agents led him past reporters to a court hearing that was closed to the public and was set to determine the terms of his custody.
Safronov, who has covered military affairs for two national newspapers, faces up to two decades in jail if found guilty. His trial is expected to be held behind closed doors because of its sensitive nature.
It is the first time in nearly two decades that a journalist has been accused of treason in Russia, said Ivan Pavlov, a prominent defence lawyer.
His detention raised fears among journalists of a new wave of repression, and staff at Kommersant, one of his former newspapers, said in an editorial that the allegations looked absurd because he was a real patriot.
Some of Safronov's former journalist colleagues protested outside FSB headquarters before being detained.
The FSB accused Safronov of working for an unnamed foreign intelligence service.
"Safronov, carrying out tasks for one of the NATO countries' intelligence services, gathered and handed over to its representative state secrets and information about military-technical cooperation and about the defence and security of the Russian Federation," it said.
Kremlin spokesman Dmitry Peskov said he did not believe the charges were related to Safronov's work as a journalist.
The space agency said Safronov had no access to secrets and the case was not related to his work there.
TASS reported last year that prosecutors wanted to bring a case against Kommersant for disclosing a state secret.
Russian news portal The Bell said then that an article which Safronov had worked on had disappeared from Kommersant's site.
It said Egypt had agreed to buy Russian Sukhoi SU-35 fighter jets. Washington threatened Egypt with sanctions if the deal went through. (Additional reporting by Alexander Marrow, Maxim Rodionov, Anton Zverev and Alexander Reshetnikov Editing by Giles Elgood and Gareth Jones)