A federal judge in New York sided Monday with Apple Inc. against the Justice Department, about whether the company can be forced to help investigators extract data from a locked phone—a ruling that could affect a similar, much-watched case involving a shooter’s phone in California.

The ruling, which could shape the broader battle about privacy, security and technology now being waged between Washington and Silicon Valley, comes a day before the head of the Federal Bureau of Investigation and Apple’s top lawyer are due to testify...