Media playback is unsupported on your device Media caption Justice Secretary Kenneth Clarke defends the government's decision

The UK government has paid compensation to 16 men who were detained by US forces at Guantanamo Bay.

Justice Secretary Ken Clarke said the settlement was confidential - but necessary to avoid a legal battle which could have cost up to £50m.

The men, 12 of whom had launched damages claims, allege that London knew or was complicit in their treatment in the wake of the 9/11 attacks.

The heads of both MI5 and MI6, who were being sued, welcomed the settlement.

The coalition government made clear in the summer that it wanted to avoid a massive court case which would also have put the British secret intelligence services under the spotlight.

Prime Minister David Cameron offered to enter settlement talks with six men seeking damages. Those talks were extended to other men and the BBC understands that one of those who settled is Shaker Aamer, who is still detained in Guantanamo Bay.

In his statement, Mr Clarke said: "I can today inform the House that the government has agreed a mediated settlement of the civil damages claims brought by detainees held at Guantanamo Bay.

Analysis The government has resisted this legal claim tooth and nail. The court hearings became an exhausting battle of legal wits, as massed benches of government lawyers refused to give ground. So why settle, given critics will inevitably accuse ministers of a cover-up? The decision should come as no surprise. The Court of Appeal rejected ministers' pleas to hear secret evidence in closed courts and the drip-drip of secret material from MI5 and MI6s vaults could have become a flood. Settling the case therefore achieves the government's greater aim. It paves the way for a judge-led inquiry into complicity and rendition. That inquiry will soon go about its business but the main accusers won't be appearing in public demanding the exposure of a secret paper trail from Afghanistan to government offices in London. Paying out millions looks bad, but ministers know that exposing state secrets is worse still. Profiles: Guantanamo Bay Britons

"The details of that settlement have been made subject to a legally binding confidentiality agreement. They have been reported in confidence to the Chairman of the Intelligence and Security Committee, and to the National Audit Office. No admissions of culpability have been made in settling these cases.

"The alternative to any payments made would have been protracted and extremely expensive litigation in an uncertain legal environment in which the government could not be certain that it would be able to defend Departments and the Security and Intelligence Agencies without compromising national security.

"This cost was estimated at approximately £30-50m over 3-5 years and in our view there could have been no inquiry until that litigation had been resolved."

In a statement issued on their behalf, the heads of both MI5 and MI6 welcomed the deal.

"Further to the Lord Chancellor's statement in the house on 16 November, Chief of SIS [Secret Intelligence Service], Sir John Sawers and Director General of the Security Service, Jonathan Evans, welcome the conclusion to the mediation which allows the agencies to concentrate on protecting national security," said the statement.

'Confidence eroded'

Mr Clarke told the Commons that there was a risk that public confidence in the UK's adherence to human rights was being eroded, including the rules governing the security and intelligence agencies.

Image caption The controversial Guantanamo Bay camp was run by US forces

The police are still investigating allegations made by some of the detainees - but Mr Clarke said that once those inquiries were complete, a judge-led inquiry could begin.

Bisher al-Rawi, Jamil el Banna, Richard Belmar, Omar Deghayes, Binyam Mohamed and Martin Mubanga had led a High Court case against five government departments, including MI5 and MI6.

They had claimed that officials in London were complicit in their transfer to Guantanamo Bay and should have prevented it and their alleged ill treatment.

In May, the Court of Appeal ruled that the government could not rely on secret evidence to defend itself against the six cases, saying allegations of wrongdoing had to be heard in public.

Since then, more than 60 government lawyers and officials have been sifting through an estimated 500,000 documents at a secret location.

Sources close to the Pentagon believe there will be two private reactions - one coming from the heart, the other the head. The initial reaction may be one of annoyance and profound embarrassment… but on reflection it will be seen as a rational decision BBC North America Editor Mark Mardell BBC - Mark Mardell's America

The Intelligence and Security Committee and the National Audit Office have been briefed about the payments, but nothing will be made public, including how much each detainee has received.

As in common in such settlements, the government has not admitted liability - and the claimants have not withdrawn their allegations.

Shadow justice secretary Sadiq Khan asked for details of the cost of the payments, saying it was in the "public interest" to know the sums involved.

But Mr Clarke told him the settlement "could be reopened if either side started breaking the confidentiality".

Shami Chakrabarti, director of human rights group Liberty, said: "This settlement could bring a broader inquiry and the end of the torture scandal a little bit closer.

"But if the slow, morale-sapping bleed of revelation and litigation is to end, the Gibson process must have all the power and authority of a court.

"It must distinguish between national security and embarrassment; between clean up and cover up."