For the last few years, American technology giants have been embroiled in a power struggle with the United States government over when authorities get to see and use the digital data that the companies collect.On Thursday, Microsoft won a surprise victory in one such legal battle against the government over access to data that is stored outside the United States.In the case, the United States Court of Appeals for the Second Circuit reversed a lower court’s ruling that Microsoft must turn over email communications for a suspect in a narcotics investigation stored in a Microsoft data center in Dublin. The case had attracted widespread attention in the technology industry and among legal experts because of its potential privacy implications for the growing cloud computing business, with implications for internet email and online storage, among other services.Had the United States government prevailed, Microsoft and others warned, it would set a dangerous precedent that would make it increasingly difficult to resist orders from foreign courts demanding data, such as email from human rights activists or political dissidents. Corporate and government customers abroad also might be unwilling to use cloud services from Microsoft if they thought their data could be seized by American courts, Microsoft said.The Justice Department, which brought the case, had argued that Microsoft’s status as a company based in the United States gave it authority to obtain its data, even if the data was stored outside the country.The case is part of the broader tussle between American technology companies and the United States government over digital data. While the companies have often invoked privacy rights to resist government interference with the data and to protect their business, law enforcement authorities have argued they need the data access for security reasons. Earlier this year, when Apple battled the F.B.I. over whether to help the agency break into a locked iPhone that had been used by a gunman in a mass shooting, the security and privacy of digital data was also invoked.On Thursday, Bradford L. Smith, Microsoft’s president, said the court’s ruling was a victory for digital privacy rights. He added that the adoption of cloud services by customers in some countries, especially in the public sector, had slowed as a result of the uncertainty around the privacy of their communications.