An attempt by the Crown Prosecution Service to hold a terrorism trial entirely in secret has been overturned by the court of appeal. The request, unprecedented in recent criminal justice history, would have prevented anyone knowing even the identity of the two men accused.

Following the ruling, it can now be reported that Erol Incedal and Mounir Rarmoul-Bouhadjar are aged 26 and from London. They have both pleaded not guilty to the charges.

The compromise reached over the highly sensitive Old Bailey case, known formerly only as the crown v AB and CD, sets fresh standards for imposing restrictions on the principle of open justice.

The ruling follows a legal challenge by the Guardian and other media which succeeded in overturning the CPS's attempt to conduct the case entirely in secret with the accused remaining anonymous.

The three judges, Lord Justice Gross, Mr Justice Simon and Mr Justice Burnett, also declared that some of the opening speeches of the trial and the final verdicts could be held in open court.

A few "accredited journalists" will, additionally, be permitted to attend the "bulk" of the trial but will not be able to report on the proceedings until there have been further legal arguments and any notes made will have to be stored in court. A transcript of the case could eventually be released but only after further legal argument.

Gross said: "We express grave concern as to the cumulative effects of holding a trial in camera and anonymising the defendants. We find it difficult to conceive of a situation where both departures from open justice will be justified … We are not persuaded of any such justification in the present case."

But the judges accepted that most of the case would have to be heard behind closed doors for reasons of national security. "This case is exceptional," Gross continued. "We are persuaded on the evidence before us that there is a significant risk – at the very least a serious possibility – that the administration of justice would be frustrated were the trial to be conducted in open court ... In our judgment, as a matter of necessity, the core of the trial must be heard in camera."

The Conservative MP David Davis said: "This is a massive improvement on the original draconian proposal that the government originally put up. It demonstrates only too clearly how over the top that demand by our agencies was in terms of the real security of the nation.

"Nevertheless we should be wary of accepting as the new norm in camera trials with controlled journalistic access. This demand for secrecy which has become habitual from the government is at odds with the traditions of our nation when the threats were much higher than they are today."Explaining their reasoning, Gross said: "Open justice is both a fundamental principle of the law and a means of ensuring public confidence in our justice system; exceptions are rare and must be justified on the facts. Any such exceptions must be necessary and proportionate."

"National security is itself a national interest of the first importance and the raison d'etre of the security and intelligence agencies, who themselves operate within a framework of law and oversight. For the [intelligence] agencies to operate effectively, at least much of their work is secret and must remain so as a matter of necessity. From time to time, tensions between the principle of open justice and the needs of national security will be inevitable.

... "Open justice must... give way to the yet more fundamental principle that the paramount object of the court is to do justice; accordingly, where there is a serious possibility that an insistence on open justice in the national security context would frustrate the administration of justice, for example, by deterring the crown from prosecuting a case where it otherwise should do so, a departure from open justice may be justified."

Incedal is charged with preparation of terrorist acts under the Terrorism Act 2006 and with collection of information contrary to the Terrorism Act 2000. Rarmoul-Bouhadjar is also charged with collecting information under the Terrorism Act 2000 as well as with possession of false documents contrary to the Identity Documents Act 2010.The judges ordered that the swearing-in of the jury, reading the charges, part of the judge's introductory remarks and part of the prosecution's opening remarks would be reportable. Verdicts at the end of the trial can be reported as well as any convictions and sentencing – although the latter could be subject to further legal restrictions.

The application for an entirely secret trial was supported by certificates from the home secretary, Theresa May, and the foreign secretary, William Hague, stating that it was required on the grounds of national security. No appeals against the ruling are anticipated. A spokesman for the attorney general said: "court recognised the strength of some of these arguments, and that the case can go ahead.The CPS has indicated it accepts the judgment of the court, and will tailor its approach to the prosecution accordingly."

The idea that a few "accredited journalists" under strict reporting restrictions could sit in on the secret evidence came from ministers. Criticising the proposals, the Conservative MP Dominc Raab told MPs in the Commons: "This morning the court of appeal overturned the government's application for a terrorism trial to be held in blanket secrecy.

"It still allows the state to hand-pick journalists to report on the case, subject to undefined conditions. The house has had no explanation of why this is necessary given existing powers like PII (public interest immunity) and the state is relying on very vague common law powers, not set and defined by elected members of this House.

"Given what is at stake in terms of principles of open justice and democracy, can we have a statement or debate in the near future?"

The leader of the house, Andrew Lansley, replied by referring to the attorney general's statement, adding: "The measures applied for by the CPS in this case were, they believed, justified in order for the trial to proceed and for the defendants to hear the evidence against them while protecting national security."

Sadiq Khan, the shadow justice secretary, said: "I welcome the court of appeal judgement which has confirmed that the cloak of secrecy of the scale proposed is not acceptable. There may be exceptional circumstances that warrant parts of cases being held in secret, but to do so for an entire case would have been unprecedented.

"The court of appeal had all the facts of this particular case at their disposal and have ordered that parts of it could and should be held in the open.This judgement is a victory for the precious open and transparent nature of our justice system and public confidence will be enhanced as a result."

Isabella Sankey, director of policy for Liberty, said: "The judges are clear that open justice is a priceless foundation of our system and faced with a blacked-out trial we now have a few vital chinks of light.

"But their wholesale deference to vague and secret ministerial 'national security' claims is worrying. Shutting the door on the core of a criminal trial is a dangerous departure from our democratic tradition."

At an earlier hearing, Anthony Hudson, representing the Guardian and other media, had told the court: "No order has ever been made which requires an entire criminal trial to be in private with the media excluded and the defendants unnamed. We submit that the orders made involve such a significant departure from the principle of open justice that they are inconsistent with the rule of law and democratic accountability."