Navy Secretary Richard Spencer was fired Sunday by Defense Secretary Mark Esper, who ordered that a Navy SEAL who was acquitted of murder be allowed to remain in the elite commando corps, the Defense Department said.

Esper asked for Spencer's resignation after President Donald Trump tweeted Thursday that Chief Petty Officer Eddie Gallagher would retain the gold Trident insignia signifying his status as a member of the Sea, Air, and Land Teams, or SEALs. Spencer told reporters Friday that he believed the review process over Gallagher's status should go forward.

In a letter to Trump, Spencer said he acknowledged his "termination," saying the president deserved a Navy secretary "who is aligned with his vision."

"Unfortunately, it has become apparent that in this respect, I no longer share the same understanding with the Commander in Chief who appointed me," Spencer wrote.

Secretary of the Navy Richard Spencer in 2017. Department of Defense via Reuters file

"In regards to the key principle of good order and discipline, I cannot in good conscience obey an order that I believe violates the sacred oath I took in the presence of my family, my flag and my faith to support and defend the Constitution of the United States."

Shortly thereafter, Trump tweeted that he was displeased not only by the way that "Gallagher's trial was handled by the Navy" but also because "large cost overruns from [the] past administration's contracting procedures were not addressed to my satisfaction."

"Therefore, Secretary of the Navy Richard Spencer's services have been terminated by Secretary of Defense Mark Esper," he wrote. He said he would nominate retired Adm. Kenneth Braithwaite, the U.S. ambassador to Norway, to succeed Spencer.

I was not pleased with the way that Navy Seal Eddie Gallagher’s trial was handled by the Navy. He was treated very badly but, despite this, was completely exonerated on all major charges. I then restored Eddie’s rank. Likewise, large cost overruns from past administration’s..... — Donald J. Trump (@realDonaldTrump) November 24, 2019

The Navy sought to eject Gallagher and four other sailors from the SEALs after Trump intervened to direct that Gallagher not be demoted following his conviction after posing for a picture with the corpse of a teenage fighter for the Islamic State militant group.

Esper directed that Gallagher retain his Trident pin, the Defense Department said in a statement.

Undersecretary Thomas Modly becomes acting Navy secretary pending Braithwaite's confirmation hearings.

Speaking with reporters while en route from Jordan, Gen. Mark Milley, chairman of the Joint Chiefs of Staff, said the Gallagher case "is closed" in his view.

Esper "made decisions for good reasons that are within his power to make decisions, and I’ll support the secretary of defense in those decisions," Milley said, adding that Esper and Trump "are all part of the process and made a decision, and as far as I’m concerned, it’s case closed now and it is time to move on and address the national security of the United States."

Milley said he still believes the military "has good order and discipline."

"We adhere to various rules and regulations and policies and laws, and always have and always will," he said. "And I think that this case obviously raises a variety of questions, but in the main I think the United States military remains and always will remain a very highly disciplined force."

The dispute flared into the open last week after NBC News and other organizations reported that the Navy was convening a review board to consider whether Gallagher should remain in the SEALs after he was convicted of posing with the ISIS fighter's corpse but acquitted of having killed the young man.

Trump tweeted Thursday that "the Navy will NOT be taking away Warfighter and Navy Seal Eddie Gallagher's Trident Pin," saying the case was "handled very badly from the beginning." Earlier, the president had overturned the Navy's decision to demote Gallagher, which would have severely affected his retirement pay.

In response, Spencer told reporters Friday that he believed Gallagher's review process should go forward. Multiple sources told NBC News that Spencer had privately told the White House that a tweet wasn't an official order and that if Trump was ordering the Navy to end the review board proceedings, he needed to do so in writing.

Navy Chief Petty Officer Eddie Gallagher and his wife, Andrea Gallagher, walk into military court in San Diego on June 24, 2019. Sandy Huffaker / Getty Images - file

The Defense Department said Sunday that Esper had learned that — contrary to what he was saying in public — Spencer privately proposed to the White House both that Gallagher's rank be restored and that he be allowed to retire as a SEAL. It said Esper was never informed of the private proposal.

The Defense Department said Esper had lost "trust and confidence" in Spencer "regarding his lack of candor over conversations with the White House."

Download the NBC News app for breaking news

In an interview on Fox News Channel's "Fox & Friends" on Sunday, Gallagher accused the Navy of seeking to strip the SEAL designation as retribution for Trump's intervention on his behalf.

"This is all about retaliation. They could have taken my Trident at any time they wanted," he said. "Now they're trying to take it after the president restored my rank."

Referring to Rear Adm. Collin P. Green, who as commander of Naval Special Warfare Command is in charge of the SEALs, Gallagher alleged: "What the admiral is doing is showing complete insubordination."

But Ray Mabus, President Barack Obama's Navy secretary, suggested that it was Gallagher who was being insubordinate.

"Here's a guy who is still on active duty. Here's a guy who is going on television to argue this case," Mabus said Sunday on MSNBC's "Up With David Gura."

"It's so dangerous for good order and discipline, so dangerous for military forces to get this politicized," Mabus said. "You simply cannot have good order and discipline. You simply cannot hold people accountable. You simply cannot have the elite fighting force if you allow things like this to happen."

Mabus said it was vital that the review board take any final action, not the administration.

"If you set this sort of precedent, then how do you tell the next SEAL that is up on charges not to go public, not to try to undermine their superiors, not to try to change a military judgment and make it a political one?"

A military jury acquitted Gallagher of murder and war crimes charges in July but convicted him of having posed with the corpse of the ISIS captive. He was ordered dropped in rank from chief to petty officer first class.

Trump reversed the order this month, directing Gallagher's restoration as chief petty officer.

Spencer, who became Navy secretary in August 2017, was the acting defense secretary for about a week in July as Esper underwent confirmation hearings in the Senate.