Defense Secretary Mark T. Esper asked for the resignation of Navy Secretary Richard V. Spencer on Sunday after losing confidence in him over his handling of the case of a Navy SEAL accused of war crimes in Iraq, the Pentagon said.

Spencer’s ouster was another dramatic turn in the story of Chief Petty Officer Edward Gallagher, a Navy SEAL who was accused of committing war crimes during a 2017 deployment. Gallagher was acquitted of murder but convicted in July of posing with the corpse of an Islamic State prisoner.

President Trump weighed in on the decision on Twitter, accusing the Navy of mishandling the Gallagher case and mentioning cost overruns in unspecified contracts.

“Secretary of the Navy Richard Spencer’s services have been terminated by Secretary of Defense Mark Esper,” Trump wrote. “I thank Richard for his service & commitment.”

Rather than criticizing how the Navy pursued the case, however, Esper said that Spencer privately proposed to White House officials that he would ensure that Gallagher retired as a Navy SEAL, with his Trident insignia, if they did not interfere with a review board convened to determine his fitness to stay in the elite force.

Spencer’s proposal to the White House — which he did not share with Esper during several conversations about the matter — contradicted his own public position on the case, chief Pentagon spokesman Jonathan Hoffman said in a statement.

Esper said in the statement that he was “deeply troubled by this conduct.”

“Unfortunately, as a result I have determined that Secretary Spencer no longer has my confidence to continue in his position," Esper said. "I wish Richard well.”

A senior defense official, speaking on the condition of anonymity because of the sensitivity of the issue, said the Pentagon verified with several sources that Spencer made the private offer to the White House.

Spencer, in a letter to Trump obtained by The Post, did not mention a disagreement with Esper about privately making a proposal to the White House. But Spencer acknowledged his termination and said he tried to ensure that legal proceedings were fair.

“Unfortunately it has become apparent that in this respect, I no longer share the same understanding with the Commander in Chief who appointed me, in regards to the key principle of good order and discipline,” Spencer wrote. “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.”

In a statement Sunday night, Senate Minority Leader Charles E. Schumer (D-N.Y.) said that he spoke to Spencer and that “I told him he’s a patriot, that he served the Navy and the nation well and he will be missed.”

Esper suggested to Trump that Kenneth Braithwaite, a retired Navy rear admiral who is currently the U.S. ambassador to Norway, be considered as the next Navy secretary, Hoffman said.

Trump later confirmed on Twitter that he will nominate Braithwaite. “A man of great achievement and success,” Trump wrote, “I know Ken will do an outstanding job!”

Esper and Army Gen. Mark A. Milley, the chairman of the Joint Chiefs of Staff, learned of Spencer’s private offer to the White House when they spoke with Trump on Friday, Hoffman said.

Spencer’s proposal came after Trump intervened in the cases of Gallagher and two soldiers on Nov. 15. Countering Pentagon recommendations, the president issued pardons to Army Maj. Mathew Golsteyn, who faced a murder trial next year, and former 1st Lt. Clint Lorance, who was convicted in 2013 in the murder of two unarmed men in Afghanistan.

Trump also reinstated Gallagher’s rank after the SEAL was demoted as punishment for posing for the photograph with the corpse.

After Trump’s intervention in the case, Rear Adm. Collin Green, the commander of Naval Special Warfare Command, moved to convene review boards for him and three other Navy SEALs to determine whether they should be ejected from the force.

That prompted speculation that Spencer might resign or be fired for standing up to Trump, and angry reactions from Gallagher and advocates for him.

“This is all about ego and retaliation,” Gallagher said on “Fox and Friends” on Sunday morning. “This has nothing to do with good order and discipline. They could have taken my Trident at any time they wanted. Now they’re trying to take it after the president restored my rank.”

Hoffman said that while Esper’s position is typically that military justice and disciplinary reviews should be allowed to play out “objectively and deliberately,” the actions over the last few days has led him to let Gallagher keep the Trident.

A lawyer for Gallagher, Tim Parlatore, greeted that news positively on Sunday night. He also expressed amazement in the turn of events leading to Spencer’s removal.

“This case is bananas,” he said. "Yes, you can quote that.”

The Pentagon did not clarify on Sunday whether Esper also will allow the other SEALs -- Lt. Cmdr. Robert Breisch, Lt. Jacob Portier and Lt. Thomas MacNeil -- to keep their Tridents. Each of them were under scrutiny for actions detailed in the Gallagher investigation.

The Pentagon also has not clarified how it will handle similar issues for Golsteyn. In an interview, he said Trump told him his intervention will make it as though his investigation never happened.

The Army revoked his Special Forces tab and took away a Silver Star he had been awarded for valor in Afghanistan, and has not said whether it will give them back. His Silver Star had been approved for an upgrade to the Distinguished Service Cross, one step down from the Medal of Honor, before it was revoked.

Spencer made his private pitch to the White House in conversations before a Thursday tweet by Trump, in which the president publicly pushed back against the Navy launching a review that could have stripped Gallagher of his Navy SEAL status.

“The Navy will NOT be taking away Warfighter and Navy Seal Eddie Gallagher’s Trident Pin,” Trump wrote. “This case was handled very badly from the beginning. Get back to business!”

On Saturday, Spencer downplayed any conflict with the president while speaking at the Halifax International Security Forum.

“Contrary to popular belief, I am still here. I did not threaten to resign,” Spencer said. “We are here to talk about external threats and Eddie Gallagher is not one of them.”

In his appearance on Fox & Friends, Gallagher said he was “overjoyed” by Trump’s intervention.

“I don’t know how many times I’ve thanked the president. He keeps stepping in and doing the right thing,” he said.