The NSA's mass collection of US phone records is being challenged in US federal courts. Reuters

The battle over the legality of the US government's mass telephone surveillance programme has swung back in favour of the National Security Agency after a federal judge ruled the activity was lawful.

A judge dismissed a lawsuit brought by the American Civil Liberties Union (ACLU), challenging the constitutionality of the NSA's programme to routinely monitor and collect call information. The ruling found that the US government's bulk collection of phone records is lawful under Section 215 of the Patriot Act and the Fourth Amendment.

The latest ruling directly contradicts the result of a similar legal challenge heard in a federal court in Washington DC earlier this month, which ruled the NSA's bulk collection programme was likely to violate the US constitution and was an "almost Orwellian" technology.

Judge William Pauling, a federal judge on the United States District Court for the Southern District of New York, said in his ruling that the NSA's use of telephone surveillance to intercept al-Qaida communications prior to 9/11, proved that its use was justified, adding: "This blunt tool only works because it collects everything."

"Technology allowed al-Qaida to operate decentralised and plot international terrorist attacks remotely. The bulk telephony metadata collection programme represents the government's counter-punch: connecting fragmented and fleeting communications to re-construct and eliminate al-Qaida's terror network."

"The ACLU argues that the category at issue – all telephony metadata – is too broad and contains too much irrelevant information. That argument has no traction here. Because without all the data points, the government cannot be certain it is connecting the pertinent ones."

Pointing out how the quality of metadata has improved since 2001, the judge continued: "There is no way for the government to know which particle of telephony metadata will lead to useful counterterrorism information ... Armed with all the metadata, NSA can draw connections it might otherwise never be able to find. The collection is broad, but the scope of counterterrorism investigations is unprecedented."

The ACLU filed the lawsuit against James R Clapper, the US Director of National Intelligence, in June, less than a week after the surveillance programme was revealed in documents published in The Guardian and obtained from Edward Snowden, an NSA contractor.

The Guardian revealed details of a secret order from the Foreign Intelligence Surveillance Court (FISC) to Verizon Business Network Services - of which the ACLU has been a customer - ordering the phone company to turn over metadata on "an ongoing daily basis". Metadata includes details of phone calls made from within the US, including whom calls are placed to and from, when those calls are made, and how long they last, but does not include recordings or transcripts of calls.

The Patriot Act's Section 215 allows the FBI to obtain secret court orders from the FISC compelling third parties to produce "any tangible thing" that is "relevant" to foreign intelligence or terrorism investigations. But the ACLU argues the surveillance programme, which collected details of every phone call made or received by Americans over a seven-year period, far exceeds this jurisdiction.

Jameel Jaffer, the ACLU deputy legal director, said: "We are extremely disappointed with this decision, which misinterprets the relevant statutes, understates the privacy implications of the government's surveillance and misapplies a narrow and outdated precedent to read away core constitutional protections."

"As another federal judge and the president's own review group concluded last week, the National Security Agency's bulk collection of telephony data constitutes a serious invasion of Americans' privacy."

The ACLU plans an appeal, and other cases are still to be heard in federal courts across the US. This may lead to the case being heard and ultimately decided on by the US Supreme Court.