SAN DIEGO — A decorated Navy SEAL facing a murder trial in the death of an Islamic State prisoner was freed from custody Thursday after a military judge cited interference by prosecutors.

The unexpected move drew gasps in a San Diego courtroom after lawyers for Special Operations Chief Edward Gallagher presented evidence to get the case dismissed because of prosecutorial misconduct.

The judge has not ruled yet on whether to throw out the case or remove prosecutors for launching an unusual effort to track emails sent to defense lawyers and a journalist to find the source of news leaks in the politically charged case. That hearing continues Friday.

The judge, Capt. Aaron Rugh, said he was freeing Gallagher as a remedy for prosecutors interfering with his Sixth Amendment right to counsel.

Gallagher's wife, Andrea, who has led a campaign to free her husband, put her hands to her face and burst into tears.

"I feel like it's a small victory on the way to the larger victory," Andrea Gallagher said outside court while her husband stood quietly by her side in his Navy whites. "He's now free because of the misconduct by these prosecutors."

He declined to comment.

A spokesman for the Navy prosecutors wouldn't comment on Gallagher's freedom or developments at the hearing.

Navy SEAL Special Operations Chief Edward Gallagher, charged with war crimes in Iraq, is shown in this undated photo provided May 24, 2019. Reuters

Defense attorney Tim Parlatore had accused prosecutors of a "rogue, relentless, and unlawful cyber campaign" that may have violated attorney-client privilege and hurt Gallagher's ability to get a fair trial.

Evidence at the hearing showed prosecutors had enlisted a Naval Criminal Investigative Service intelligence specialist to conduct criminal background checks on three civilian lawyers, including Parlatore, and a journalist with the Navy Times who has broken several stories based on leaked documents.

The defense said most of the articles based on leaks have been favorable to the prosecution, yet the investigation did not target prosecutors or NCIS investigators.

Efforts to get the case thrown out come as President Donald Trump considers pardoning several service members accused of war crimes, including Gallagher. Gallagher faces trial June 10.

Trump previously got Gallagher removed from the brig and transferred to better custody conditions at a Navy hospital.

During the hearing Thursday, Rugh indicated he was kept in the dark by prosecutors about the email monitoring.

Rugh said prosecutors told him privately they planned to embed code in what he believed to be a court document to help them find the source of leaks. But he said he didn't have the power to authorize such an investigative tool, and wasn't told they planned to plant the code in emails sent to defense lawyers or a journalist.

Rugh said he thought prosecutors were coordinating the investigation with the U.S. attorney's office in San Diego. Rugh said he wasn't aware that a federal prosecutor told the military prosecutor to make sure they had the judge's approval before launching the tracking effort.

Parlatore withdrew his motion to have the judge removed from the case after learning he had not authorized the scheme.

The lead prosecutor downplayed the move at a related hearing earlier in the day. Cmdr. Christopher Czaplak said the code embedded in the email recorded nothing more than where and when messages were opened by recipients.

Gallagher has pleaded not guilty to murder in the death of an injured teenage militant in Iraq in 2017 and attempted murder for allegedly picking off civilians from a sniper's perch.

His platoon supervisor, Lt. Jacob Portier, is fighting charges of conduct unbecoming an officer for allegedly conducting Gallagher's re-enlistment ceremony next to the militant's corpse.

Czaplak said the tracking ended May 10 after he was confronted by defense lawyers who discovered the code in an unusual logo of an American flag with a bald eagle perched on the scales of justice beneath Czaplak's signature.

Czaplak acknowledged the effort in a closed-door hearing three weeks ago. He disclosed no other details at the time.

On Thursday, Czaplak said the emails were similar to what marketers use to see when an email is opened and what device was used to open it.

"It's still a web bug and it's still unethical" countered defense lawyer Jeremiah J. Sullivan III, who represents Portier.

The judge in Portier's case, Capt. Jonathan Stephens, said from what he had seen the tracking effort wasn't able to view the contents of any emails.

Several experts testified that the code embedded in a signature line in the emails collected information on internet protocol addresses and could tell what web browser was being used, the duration it was open and could see if the message had been forwarded. But the information couldn't generally be used to identify a specific person or capture content.

Josiah Roloff, a data forensics examiner from Spokane, Washington, said the code is typically used by marketing companies and he's seen it used in undercover investigations. But he said he's never seen it used to target defense lawyers.