A major criminal trial involving two men charged with serious terrorism offences could be held entirely in secret for the first time in modern British legal history.

Lawyers contesting the decision at the court of appeal on Wednesday said the plan amounted to "an unprecedented departure from the principles of open justice" and was "inconsistent with democracy and the rule of law".

Until now it has not even been possible to report the existence of the forthcoming trial against the two men, known only as AB and CD. But three appeal court judges lifted a gagging order allowing reporting of a hearing challenging the plans.

The trial would be the first criminal case to be held behind closed doors for hundreds of years. It involves two defendants who are charged with terrorism but whose names are being withheld from the public. Unless the appeal succeeds, journalists will be banned from being present in court to report the proceedings on 16 June or the outcome of the trial.

The men will be tried by a jury but no report of the case will be made public and no members of the media or public will be given access to the court.

Shami Chakrabati, director of Liberty, condemned the secrecy. She said: "Transparency isn't an optional luxury in the justice system – it's key to ensuring fairness and protecting the rule of law.

"This case is a worrying high water mark for secrecy in our courts – extensive restrictions set without robust reasons or a time limit. There must be clearer explanations before the door is shut on press and public."

The Guardian and other media organisations made a last-ditch challenge to the secrecy surrounding the terror trial at the court of appeal. Anthony Hudson, representing the media, said the decision to withhold the identities of the men and carry out the entire proceedings in private was a legal departure.

The court was told that the crown has sought and obtained legal orders on the grounds of national security, arguing that if the trial were held in public the prosecution might not proceed with the case.

Mari Reid, unit head of the counter-terrorism team in the special crime and counter-terrorism division of the Crown Prosecution Service, gave evidence during the crown application that there was a "serious possibility that the trial may not be able to go ahead" if it had to be held in public.

Counsel for the crown detailed evidence to back its case in private with the three court of appeal judges: Lord Justice Gross, Mr Justice Simon and Mr Justice Burnett.

Appealing against orders made by Mr Justice Nicol at an earlier hearing last month, Hudson said the secrecy around the trial was wholly unprecedented. He said: "The crown has sought and obtained unprecedented orders" and the trial "should take place entirely in private".

He added: "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."

He said that national security could not be pursued without regard to the values of the society it was seeking to protect.

Richard Whittam, QC, for the crown, told the court the case involved clearly exceptional circumstances which had led to the "exceptional procedures" that had been approved by Mr Justice Nicol on 19 May.

He said while the crown entirely supported open justice, the exceptional nature of the case made it necessary for the unprecedented procedures to be put in place.

"It is quite clear that there is jurisdiction for the defendants to be anonymous and there is jurisdiction for a court to sit in private. Whether or not it is appropriate to do so is evidence-dependent," he said.

But the evidence on which the crown relied to argue for the secret trial could not be presented in open court, he added. Whittam instead presented the evidence in private to the appeal court judges during part of the hearing on Wednesday.

The court heard that AB and CD were arrested in a high-profile police operation last year and had been charged with serious terrorism offences.

AB is charged with engaging in conduct in preparation of terrorist acts between February 2012 and October 2013. He is further charged with CD of being in possession of of documents or records containing information of the kind likely to be used by a person committing or preparing an act of terrorism.

This relates to the pair's alleged possession of a document entitled "Bombmaking". CD is also charged with possession of an improperly obtained UK passport.

Lord Justice Gross said he would give the court's decision on the appeal by the media in a few days and a full judgment at a later date.

The trial of AB and CD is due to open in London on 16 June.