The ruling could signal Apple is on sound footing in a separate but similar battle with the US government over being forced to help crack into an iPhone used by one of the shooters in December's San Bernardino attacks.
US Magistrate Judge James Orenstein in New York said in a 50-page opinion that law enforcement lacked the authority to compel Apple to comply.
"The relief the government seeks is unavailable because Congress has considered legislation that would achieve the same result but has not adopted it," he wrote.
The US Drug Enforcement Agency and the FBI went to court to compel Apple to help it break into an iPhone confiscated in June of 2014 from a suspected methamphetamine trafficker, according to court documents.
The US government sought to get Apple to help break into the iPhone under the auspices of the All Writs Act, which is what is being relied on in the San Bernardino case a 1789 law that gives wide latitude to law enforcement.
Orenstein refused to issue the order in the case, saying it undermined Constitutional principles and was a matter to be dealt with by Congress.
"This is precisely on point in the San Bernardino case," a senior Apple executive said during a telephone briefing with reporters after the New York ruling was issued.
Apple and FBI are locked in battle over a warrant seeking to force the technology company to help unlock the iPhone used by one of the shooters in December's San Bernardino attacks.
Apple's refusal has set off an intense political debate about encrypted devices that provide "keys" only to users.
Non-binding precedent
Orenstein's ruling was a precedent, but the judge presiding over matters in the San Bernardino case is not bound by his decision.
Conflicting rulings on the same points of law would increase the chances of the US Supreme Court being called upon to make the final call.
Oral arguments in the San Bernardino case are to be made in federal court in Southern California on March 22.
The Apple executive said that while the magistrate judge in California is free to rule differently, the iPhone maker hoped that she would be persuaded by Orenstein's "thoughtful and masterful" reasoning in his decision.
Apple argued that it is being asked to go further in the California case by creating software that doesn't exist to essentially "hack our own phones."
Apple wants a "conversation" to help settle a standoff with US law enforcement over accessing an encrypted iPhone, according to testimony prepared for a congressional hearing.
In a statement prepared for the Tuesday hearing, Apple general counsel Bruce Sewell said the public should understand that "encryption is a good thing, a necessary thing" even if it makes the work of law enforcement more difficult.
In his remarks, Sewell said Apple has been stepping up its encryption over the past few years.
"As attacks on our customers' data become increasingly sophisticated, the tools we use to defend against them must get stronger too," he said.
"Weakening encryption will only hurt consumers and other well-meaning users who rely on companies like Apple to protect their personal information."
Encryption helps preserve privacy around the world, he added, "and it keeps people safe."
Lawmakers and the public should decide the question of access to the locked iPhone, Sewell said, renewing Apple's criticism of the government's use of the broad 1789 All Writs Act, which offers broad authority to law enforcers.
Sewell repeated comments by Apple chief Tim Cook that the FBI is asking the company "to create an operating system that does not exist" that would open "a backdoor into the iPhone."
But New York District Attorney Cyrus Vance argued in his own prepared testimony that the current level of encryption "cripples even the most basic steps of a criminal investigation."
For the latest tech news and reviews, follow Gadgets 360 on X, Facebook, WhatsApp, Threads and Google News. For the latest videos on gadgets and tech, subscribe to our YouTube channel. If you want to know everything about top influencers, follow our in-house Who'sThat360 on Instagram and YouTube.