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

The unexpected move drew gasps from those at a hearing in a San Diego courtroom in which lawyers for Special Operations Chief Edward Gallagher sought to get the case dismissed for prosecutorial misconduct.

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

The judge, Capt. Aaron Rugh, has not ruled yet on whether to throw out the case or remove prosecutors over tracking emails sent to defense lawyers and a journalist in an effort to find the source of news leaks in the politically charged case. He said freeing Gallagher at this point would be a remedy for interference by prosecutors.

Motion filed to dismiss lead prosecutor and possibly judge in SEAL war crime trial “If the Military Judge authorized an investigation with such little knowledge, that is, at the very least, quite unnerving,” the motion reads.

Attorney Tim Parlatore 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.

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.

× Fear of missing out? Sign up for the Navy Times Daily News Roundup to receive the top Navy stories every afternoon. Thanks for signing up. By giving us your email, you are opting in to the Navy Times Daily News Roundup.

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

Rugh said prosecutors had 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 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 recorded nothing more than where and when email messages were opened by recipients that included a journalist for the Navy Times.

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.

Defense lawyer Jeremiah J. Sullivan III, who represents Portier, said there was no probable cause for the tracking effort, which was discovered in an unusual logo of an American flag with a bald eagle perched on the scales of justice beneath Czaplak's signature.

Czaplak said the tracking ended May 10 after he was confronted by defense lawyers and acknowledged the effort in a closed-door hearing. He disclosed no other details at the time.

On Thursday Czaplak said the emails contained code 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," Sullivan said.

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.

Melley reported from Los Angeles.