Defense Secretary Mark T. Esper on Monday explained why he ousted his Navy secretary, saying he was “flabbergasted” to learn that Richard V. Spencer tried to make a secret deal with the White House involving a Navy SEAL accused of war crimes.

Esper said that without his knowledge, Spencer promised the White House that the SEAL could retire as a member of the elite force if President Trump stayed out of the case. That outcome aligned with Trump’s wishes, but Esper said that he and Gen. Mark A. Milley, the chairman of the Joint Chiefs of Staff, felt “blindsided” by Spencer’s back-channel conversations.

“First, we have a chain of command that should be followed, and that chain of command must be kept informed,” he said. “Second, once we agree on a position, we stick to it and support it both in private and public.”

Esper, speaking to reporters at the Pentagon, said Spencer’s plan contradicted what he had told senior defense officials in recent days — that he was considering resigning if Trump forced the issue.

As a result, the defense secretary said, Spencer had to go.

On Monday night, Spencer defended his actions in an interview with “CBS Evening News.” In his first comments since being removed, he said that Trump’s intervention on behalf of Chief Petty Officer Edward Gallagher sends a message to U.S. troops that they can “get away” with bad behavior.

“We have to have good order and discipline,” Spencer said. “It’s the backbone of what we do.”

Spencer said that he would take responsibility for making the offer to the White House without the defense chief’s knowledge but that Esper’s chief of staff, Eric Chewning, was informed.

The Pentagon press secretary, Alyssa Farah, denied that anyone on Esper’s staff was aware that Spencer had discussed any such deal with the White House senior staff.

Spencer’s dismissal is the latest issue to roil the Pentagon in relation to Trump’s Nov. 15 decision to intervene in three high-profile war crimes cases, including Gallagher’s, despite opposition from senior Defense Department officials.

Trump’s intervention set off a series of reactions: The Navy convened review boards to decide whether to eject Gallagher and three other Navy SEALs from the force, the Pentagon tried to talk the president out of getting involved further, and White House officials began questioning Esper’s loyalty to Trump, according to several administration officials, who spoke on the condition of anonymity because of the sensitivity of the issue.

It remains uncertain how the Army will handle the cases of the two soldiers Trump pardoned, and how a president deeply interested might react.

The situation in the Navy began a new slow boil after Trump reinstated the rank of Gallagher, who was acquitted of murder in July but convicted of posing with the corpse of an Islamic State fighter and demoted. Murder allegations were brought against him by several of his SEAL teammates, but the case fell apart at trial after a SEAL testified under immunity that he actually had killed the prisoner in question.

After Trump’s intervention, Rear Adm. Collin Green, the commander of Naval Special Warfare Command, decided to convene review boards to determine whether Gallagher and three officers who oversaw him in Iraq could keep their Trident, the insignia representing their connection to the SEALs.

Spencer’s private offer to the White House occurred around the same time, while Esper was traveling in Asia.

It wasn’t clear Monday whether Trump considered Spencer’s alleged proposal, but the president made his views on the case known last Thursday.

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

Trump sent the tweet because he believed that the military was trying to embarrass him, a senior administration official said. Changing the president’s mind then became a “top priority” for Pentagon leadership, said three administration officials with knowledge of the situation.

Esper said Monday that Army and Navy leaders agreed a few weeks ago that the Pentagon should rely on the military’s legal system and administrative processes to resolve the cases. A day after the news broke that Trump might soon intervene, Esper, Milley, Army Secretary Ryan McCarthy and other defense officials sought to change the president’s position, several officials familiar with the situation have said.

The defense chief said he learned from a senior White House official on Friday after meeting with Trump that Spencer had offered an alternative option.

Esper and Milley had just made the case to Trump that he should let the system run its course and told him that if Gallagher lost his Trident, the president could reverse the decision as commander in chief, according to an administration official. They left the White House believing that the president might agree to wait, the official said.

Esper said that he told Trump on Saturday that he would ask for Spencer’s resignation and that the president supported his decision. On Sunday, Trump directed Esper to allow Gallagher to retire as a SEAL, and Esper said he would do so.

Esper said he believes it was a lawful order, and therefore he must either follow it or resign. He also made the case that leaving the issue to a review board composed of Navy SEALs would be unfair to them.

“While I believe strongly in process, the issue should not now be thrown into the laps of a board of senior [enlisted sailors] to sort out,” Esper said. “As professional as they are, no matter what they would decide they would be criticized from many sides, which would further drag this issue on, dividing the institution. I want the SEALs and the Navy to move beyond this now and get fully focused on their warfighting mission.”

In a letter to Trump acknowledging his dismissal, Spencer 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.”

Trump tweeted about Spencer on Sunday night after his termination letter began circulating, saying he was not happy with the Navy’s handling of the Gallagher case or cost overruns on some unnamed contracts. Spencer’s letter annoyed the president, who wanted to frame the issue in his own terms, another administration official said.

Esper said the Gallagher case has “dragged on for months, and it has distracted too many.”

“If folks want to criticize anyone about reaching down into the administrative processes, then simply blame me,” he said. “I’m responsible at this point. It’s not where I prefer to be, but I’ll own it.”

Esper declined to name the senior White House official who revealed Spencer’s proposal to him but said that Spencer was “forthright” when he questioned him about it.

The defense secretary said Monday that he cannot reconcile Spencer’s comments, including in his termination letter, with his private promise to ensure that Gallagher would be able to retire as a SEAL.

“This is my issue with trust and confidence,” Esper said. “I and General Milley and others had been acting on good faith and confidence with regard to rules and agreements we set forth. And then to find out that for some manner of time our position was being undermined and that at some point in time somebody in their chain of command might be asked to compromise their integrity and bend the rules? My view has been, let the process play itself out.”

It was not clear whether the three SEAL officers also facing review boards in the case — Lt. Cmdr. Robert Breisch, Lt. Jacob Portier and Lt. Thomas MacNeil — will receive a reprieve. Esper said he has asked Navy leaders to bring him a recommendation.

The Army, meanwhile, has not said how it will handle Army Maj. Mathew L. Golsteyn and 1st Lt. Clint Lorance, the soldiers Trump pardoned. Golsteyn was due to be tried next year on a murder charge in the case of a suspected Taliban bombmaker, while Lorance, who has been dismissed, was convicted in 2013 in the murder of two unarmed Afghans.

Golsteyn is seeking the reinstatement of his Special Forces tab and the pinning of a Distinguished Service Cross, which is one step down from the Medal of Honor. The Army revoked the tab and a Silver Star awarded to him after the investigation began. The Silver Star had been approved for an upgrade.

Esper said Monday that he is “going to take this one step at a time” and that he still is focusing on Gallagher’s case.