BARCELONA, Spain — Mark Zuckerberg on Monday threw his support behind Apple in its bitter battle with the United States government over attempts to gain access to information protected by the company’s sophisticated encryption technology.

Speaking at the Mobile World Congress, an annual tech and telecommunications conference here, Mr. Zuckerberg said that Facebook would play its part in the fight against global terrorism, but that weakening the digital security of technology companies was a bad idea.

A federal magistrate judge, at the Justice Department’s request, recently ordered Apple to bypass security functions on the phone of a gunman in the December mass shooting in San Bernardino, Calif. But the company has refused to comply with that order, fearful that it could be forced to create a so-called back door to its encryption technology that could be misused by government agencies.

“I don’t think building back doors is the way to go, so we’re pretty sympathetic to Tim and Apple,” said Mr. Zuckerberg, in reference to Apple’s chief executive, Timothy D. Cook.