A specially-convened committee of nine law lords upheld an appeal by nine foreigners who have been detained without charge or trial, most of them in Belmarsh prison, south-east London, for around three years.

The decision by the law lords, Britain's highest court, throws the government's security policies into chaos and was a blow for Charles Clarke on his first day as home secretary following the resignation of David Blunkett last night.

There was pressure on Mr Clarke to release the detainees but he remained defiant, insisting that they would remain in prison while parliament decided how to react to the ruling.

Mr Clarke said he had "reason to believe [the detainees] are a significant threat to our security". He said: "My primary role as home secretary is to protect national security and to ensure the safety and security of this country."

Civil liberties groups said the ruling should force the government to repeal the section of the Anti-Terrorism, Crime and Security Act 2001 which has permitted the indefinite detention of foreigners.

The law lords have no powers to strike out the act but experts said their ruling had left the government in an impossible position.

Mr Clarke said he would ask parliament to renew the legislation in the new year and in the meantime he would be studying the judgment to see whether it was possible to modify the legislation "to address the concerns raised by the House of Lords".

The anti-terror legislation was brought in by Mr Blunkett in the wake of the September 11 2001 terror attacks.

The law lords, making the ruling in the chamber of the House of Lords, described the legislation as "draconian" and "anathema" to the rule of law. One of the law lords, Lord Hoffmann of Chedworth suggested that the act itself was a bigger threat to the nation than terrorism. In a statement after the ruling, one of the detainees, known only as "A", who is being held at Woodhill prison, Milton Keynes, said: "I am very pleased ... [this] proves that however erroneous the policies of the government are there will always be an independent judiciary that will be there to say that enough is enough."

The detainees had taken their battle to the House of Lords after the court of appeal backed the Home Office's powers to detain them without limit or charge. Only law lords, rather than lay members of the House of Lords, can give rulings on appeals.

Lawyers for the detainees had challenged the lawfulness of Britain's opt-out of Article 5 of the European Convention on Human Rights, the right to liberty. The convention guarantees the right to be brought to trial within a reasonable time or be released.

Today the law lords ruled eight-to-one in favour of the detainees after hearing arguments that detaining people indefinitely on suspicion alone contravened democratic rights and international obligations.

Lord Bingham of Cornhill, the senior lord of appeal and former lord chief justice, said that the powers under which the men were held were incompatible with European human rights laws because they "discriminate on the ground of nationality or immigration status".

Lord Nicholls of Birkenhead ruled that: "Indefinite imprisonment without charge or trial is anathema in any country which observes the rule of law. It deprives the detained person of the protection a criminal trial is intended to afford."

Lord Bingham ordered the government to pay the legal costs of the appellants.

The civil rights group, Liberty, welcomed the ruling, saying that in the future if the government wanted to hold someone on terrorism charges, it would have to produce evidence.

"We just hope that Charles Clarke now pays attention and restores a sense of basic decency to our justice system," a spokesman for Liberty said.