A New York judge has ruled that the National Security Agency's collection of millions of Americans' telephone calls is lawful, rejecting a challenge to the controversial counter-terrorism programme by the American Civil Liberties Union.

US District Judge William Pauley ruled on Friday that the NSA programme "represents the government's counter-punch" to eliminate al-Qaeda, and said the programme's constitutionality "is ultimately a question of reasonableness".

In a 54-page decision, Pauley said there was no evidence that the government had used the data acquired by the NSA for any reason other than to investigate and disrupt "terrorism".

He added that the NSA programme could have helped investigators gather information to prevent the 9/11 attacks on the Twin Towers in New York City in 2001.

"The government learned from its mistake and adapted to confront a new enemy: a terror network capable of orchestrating attacks across the world,'' Pauley wrote.

The ruling contradicted an earlier US District Court decision from December 17.

In that case, Judge Richard Leon found the NSA programme likely violated the US Constitution's ban on unreasonable search, and granted a preliminary injunction against the collecting of the phone records of two Americans.

Al Jazeera's Cath Turner, reporting from New York, said: "If this conflict continues in the lower courts, in federal courts, we could well see the Supreme Court making the ultimate decision and put this to rest once and for all in terms of whether this is actually legal or not according to American law."

Tapping defended

On Friday, Judge Pauley denied the ACLU's motion for a preliminary injunction, and granted a government motion to dismiss the case.

The federal case came after whistleblower Edward Snowden revealed the NSA was picking up millions of telephone and internet records that are routed through US networks each day. The secret programme, critics say, violates privacy rights.

In October, media reports accused the NSA of also collecting tens of millions of European phone records, and spying on political leaders, such as German Chancellor Angela Merkel.

US President Barack Obama has defended the surveillance programme, but indicated a willingness to consider constraints.

The ACLU and the White House had no immediate comment on Friday's ruling.

"We are pleased with the decision," said Justice Department spokesman Peter Carr.

Republican Congressman Peter King, chairman of the House Homeland Security Subcommittee on Counterintelligence and Terrorism, said in a statement that Pauley's decision "preserves a vital weapon for the United States in our war against international terrorism".