WASHINGTON—A federal appeals court Friday reversed a judge’s order to stop the National Security Agency from collecting phone records, setting the stage for further legal battles between privacy advocates and government lawyers.

The three-judge panel’s ruling comes after Congress has already passed legislation ending the government’s collection of bulk phone records. That program will be replaced by a system in which the phone companies retain the data, which can be searched by the government only when authorized by a court.

...