US magistrate Judge Sheri Pym ordered Apple on Tuesday to provide "reasonable technical assistance" to the FBI, including disabling an auto-erase feature after too many unsuccessful attempts are made to unlock the iPhone 5c.
Federal prosecutors had filed a motion requesting Apple's help after the FBI failed to crack the phone's code two months into the investigation into the December rampage.
Syed Farook, a US citizen, and his Pakistani wife Tashfeen Malik gunned down 14 people at an office party in San Bernardino, California, before they were killed in a shootout with police.
But Apple said it would fight the judge's order, firing the latest shot in a growing debate over encryption pitting the government against tech companies.
"The United States government has demanded that Apple take an unprecedented step which threatens the security of our customers," Apple chief executive Tim Cook said in a statement on the company's website.
"We oppose this order, which has implications far beyond the legal case at hand."
Cook said it was too risky to provide the requested software because it could allow ill-intentioned individuals to unlock any iPhone and raises major privacy concerns.
"The US government has asked us for something we simply do not have, and something we consider too dangerous to create. They have asked us to build a backdoor to the iPhone," Apple said.
"In the wrong hands, this software which does not exist today would have the potential to unlock any iPhone in someone's physical possession.
"While the government may argue that its use would be limited to this case, there is no way to guarantee such control," he said, adding that Apple has cooperated with the FBI thus far.
By disabling the security features, the FBI would be able to attempt as many different password combinations as needed before gaining access to the phone.
It was the property of the San Bernardino County Department of Public Health, which employed Farook, and the authority had agreed to the search of the phone.
'Chilling'
Pym ordered Apple to provide software that would only run on the device in question, or any other technological means to access its data.
But Apple said it was impossible to create such a tool that could only be used once, on one phone.
"Once created, the technique could be used over and over again, on any number of devices," Apple said.
"In the physical world, it would be the equivalent of a master key, capable of opening hundreds of millions of locks from restaurants and banks to stores and homes. No reasonable person would find that acceptable."
The US government is concerned that commercially-available encryption benefits criminals.
Tech companies, intent on securing the trust of consumers after government spying revelations made by Edward Snowden, have been reluctant to be seen as helping authorities spy on users.
"We can find no precedent for an American company being forced to expose its customers to a greater risk of attack," Apple said.
"The implications of the government's demands are chilling."
"If the government can use the All Writs Act to make it easier to unlock your iPhone, it would have the power to reach into anyone's device to capture their data."
Cook warned that if Apple complied with the order, the government could demand surveillance software to intercept, access health and financial data, track users' location or access a phone's microphone or camera without the user's knowledge.
"We are challenging the FBI's demands with the deepest respect for American democracy and a love of our country," Cook added.
US Attorney Eileen Decker had earlier called the order "another step a potentially important step in the process of learning everything we possibly can about the attack in San Bernardino."
FBI Director James Comey revealed last week that investigators had not been able to crack open the phone two months into the 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.