A federal magistrate-judge in New York City has ruled that the U.S. government can't force Apple to hack an iPhone to investigate a drug dealer.

It's a win for Apple, which is being pressured by federal law enforcement agents to help it break into iPhones in at least 13 instances across the country. Apple says doing the federal government's bidding would undermine the security features in hundreds of millions of iPhones around the world.

So far, the Department of Justice is relying on the All Writs Act, passed in 1789, which gives judges broad discretion in carrying out the law.

Related: DOJ says Apple had routinely helped law enforcement

But on Monday, Judge James Orenstein said federal investigators can't use that law to pull this off.

The U.S. government's argument doesn't justify "imposing on Apple the obligation to assist the government's investigation against its will," the judge wrote.

Orenstein said law enforcement is inappropriately trying to use powers that it hasn't been given by Congress.

"[T]he question to be answered in this matter, and in others like it across the country, is not whether the government should be able to force Apple to help it unlock a specific device," Orenstein wrote. "[I]t is instead whether the All Writs Act resolves that issue and many others like it yet to come. ... I conclude that it does not."

This particular criminal case involves a methamphetamine dealer, Jun Feng, who was arrested in 2014 and cut a plea deal with prosecutors. Last year, the Drug Enforcement Agency got a search warrant to look through Feng's iPhone 5C to track down his fellow drug dealers and customers. But the device is running the iOS 7, and agents couldn't crack the passcode to see the data inside.

The DEA asked for Apple's help. Apple initially said it would help -- but now it's unwilling. The U.S. Department of Justice claims Apple is being inconsistent.

"Apple ... only changed course when the government's application for assistance was made public by the court," the department said in a statement on Monday, implying that Apple is merely doing this to save face and seem protective of customers' privacy.

On a call with reporters, an Apple senior executive said Monday the company did offer to help -- but only if the U.S. government makes a lawful request.

"We will produce information when there is a lawful order to do so," the executive said. "But Judge Orenstein, on his own behalf, said he would not issue this order."

Federal prosecutors plan to object to Orenstein's decision and take it up with the U.S. district judge overseeing the case.

"This phone may contain evidence that will assist us in an active criminal investigation and we will continue to use the judicial system in our attempt to obtain it," the department said.

American Civil Liberties Union staff attorney Alex Abdo called the ruling "a victory for privacy, security, and common sense."

Apple (AAPL) is facing pressure in 10 other cases nationwide to help agents break into 13 phones.

These cases are being closely watched, because they pit the federal government against the richest tech company on the globe. They raise serious questions about the authority of law enforcement to force companies to help in hunting down criminals.

In a well-publicized case involving the FBI trying to break into an iPhone 5C belonging to one of the San Bernardino shooters, Apple is arguing the U.S. government can't force it to write code.

--CNN's Laurie Segall contributed to this report.