Two terror suspects ruled to be a threat to national security will be freed within days after the government suffered a major blow to its counter-terrorism policy.

The Libyans won their appeals yesterday against Home Office attempts to deport them.

It was revealed for the first time that one of the men, who can be identified only by the initials DD, was found with a map marked with the flightpath to Birmingham International Airport.

He is also related to two other Islamic terrorists, including the suspected mastermind of the Madrid train bombings which killed 191 people.

The Special Immigration Appeals Commission (Siac) in London allowed the appeals by DD and another man, AS, granting them bail in principle under strict conditions, including a daily 12-hour curfew.

The Home Office opposed bail on the grounds they would abscond if let out of Long Lartin maximum security prison, where they have been under immigration detention.

However, Siac Judge Mr Justice Mitting said keeping them in detention after they had won their appeal would be on the "cusp of legality".

Tim Eicke, representing John Reid, Home Secretary, said the Home Office would appeal against Siac's decision.

But Mr Justice Mitting said: "My provisional view is that the prospects of successfully applying for permission and of challenging the decision of the commission on a point of law in the Court of Appeal are scant."

Rasa Hussain, representing the two Libyans, suggested the Home Secretary's case for disputing Siac's ruling was "threadbare".

It was the first test of a crucial diplomatic agreement, known as a "memorandum of understanding", or MOU, which the government signed with Tripoli to remove suspects without breaching human rights laws.

Mr Justice Ouseley, Siac chairman, said in his ruling that there remained a real risk that the European Convention on Human Rights would be breached if the two men were removed to Libya.

He added: "There is also real risk that the trial of the appellants would amount to a complete denial of a fair trial."

Colonel Muammar Gadaffi's regime may not be able to resist the temptation to interrogate the men, he added.

MOUs are a key plank of the Home Office's policy on dealing with foreign terror suspects who cannot be prosecuted in UK courts.

A series of agreements have been signed with Middle Eastern and North African countries, seeking guarantees that any of their citizens deported by the UK will not face torture or ill-treatment.

A Home Office spokesman said: "We are very disappointed with today's decision by Siac. We believe that the assurances given to us by the Libyans do provide effective safeguards for the proper treatment of individuals being returned and do ensure that their rights will be respected.

David Davis, Shadow Home Secretary, said: "This shows that MOUs are no panacea to the problem we face.

"The government would do well to focus on prevention and prosecution rather than just trying to deport these individuals once they are here."

Nick Clegg, Liberal Democrat home affairs spokesman, said: "It is vital that people should not be deported to countries where they face torture or cruel, inhuman or degrading treatment."

The court ruled that DD was a "real and direct threat to the national security of the UK" and a "global jihadist with links to the Taliban and al Qaeda".