David Petraeus pleaded guilty last year to mishandling classified information; he had given his biographer, who was also his mistress, sensitive material. (Mark Wilson/Getty Images)

Inside a secure conference room on the sixth floor of the Justice Department in early 2014, top federal law enforcement ­officials gathered to hear what criminal charges prosecutors were contemplating against David H. Petraeus, the ­storied wartime general and former CIA director whose public career had ended about 15 months earlier over an extramarital affair.

Attorney General Eric H. Holder Jr. and FBI Director James B. Comey listened as prosecutors did a mock run-through of the government’s case, a preview of how they would present their evidence to Petraeus’s lawyers in order, they hoped, to force a guilty plea.

The presentation included felony charges: lying to the FBI and violating a section of the Espionage Act. A conviction on either carried potentially years in prison.

They were also considering bringing the same charges against Petraeus’s biographer and former mistress, Paula Broadwell.

The government would never file those charges. Not everyone at Justice shared the prosecutors’ confidence, and lawyers for Petraeus and Broadwell separately pushed back hard, saying they would fight and beat the charges being considered. Moreover, with its mix of sex and government secrets, a trial promised to be an uncomfortably tawdry affair, one some in the government — as well as defense lawyers — preferred to avoid.

In his first public testimony since resigning as CIA director, former Army Gen. David H. Petraeus apologized to the Senate Armed Services Committee for his extramarital affair. (AP)

Petraeus, in the end, pleaded guilty last year to a misdemeanor charge of mishandling classified material. No charges were brought against Broadwell.

[Petraeus pleads guilty, will face probation]

The Justice Department has never discussed how it reached its decision to accept a plea on the lesser charge. But six current and former U.S. officials, as well as others familiar with the case, provided the first detailed look at the internal debates and wrangling with Petraeus’s lawyers that took place before the retired four-star general entered his guilty plea in federal court in Charlotte. All spoke on the condition of anonymity to discuss private legal deliberations.

As part of the agreement, Pet­raeus admitted that he improperly removed and retained highly sensitive information in eight personal notebooks that he gave to Broadwell. The Justice Department said the information, if disclosed, could have caused “exceptionally grave damage.” Officials said the notebooks contained code words for secret intelligence programs, the identities of covert officers, and information about war strategy and deliberative discussions with the National Security Council.

The plea agreement left some in the Justice Department angry, particularly at the FBI, and some agents have argued privately that it will hamper future efforts to secure prison terms in leak cases. But others in the government defended the deal as the only viable conclusion to a case in which a successful prosecution on the more serious charges was far from certain.

“Nobody was going to be happy with the outcome,” a former Justice Department official said. “There was nothing about this case that was typical.”

The plea agreement, while helping Petraeus avoid the prospect of prison, probably has ended whatever ambition he had to become president. It also does not protect him from further punishment — such as stripping him of a star — by the military. The Army recently recommended that Petraeus not face further punishment, but the final decision rests with Defense Secretary Ashton B. Carter, who is considering how to rule, according to Pentagon officials.

1 of 24 Full Screen Autoplay Close Skip Ad × Scandal-plagued politicians: Where are they now? View Photos The rise and fall of former Va. governor Robert F. McDonnell seems cut and dried at the moment, but public figures have a way to bounce back from scandal. Here’s a look at a few once-disgraced politicians — and what they are doing now. Caption Scandalized public figures have a way of bouncing back. Robert F. McDonnell — THEN The Virginia governor was considered a possible GOP vice presidential candidate until gifts from a major donor prompted investigations. Steve Helber/AP Buy Photo Wait 1 second to continue.

“I have turned the page on these matters, I am looking forward, and I will have no further comment. I resigned as CIA Director, publicly apologized for my conduct, and formally accepted responsibility,” Petraeus, 63, said in a statement. “I served my country for over 38 years, including five combat commands during my final decade in uniform. I will leave it to the public and to history to judge that record. Beyond that, I will always regret the mistakes I made, and I will always be grateful to those who have supported me.”

A Justice Department spokesman declined to comment.

Following an email trail

In October 2012, Sean Joyce, the deputy director of the FBI, informed Petraeus, then CIA director, that a pair of FBI agents out of Tampa needed to come to agency headquarters to talk to him. They were investigating a cyber-stalking case.

The FBI had discovered that Broadwell had sent anonymous emails to a Tampa socialite named Jill Kelley and her husband months earlier. Kelley, who described the emails as harassing, contacted a friend who was an agent in the FBI’s Tampa field office.

The FBI became interested because the emails contained information about Petraeus’s schedule, raising concerns about a possible threat to the CIA director. As the investigation widened and Broadwell, now 43, was identified, the bureau discovered that she had also sent anonymous emails to military officials — and appeared to be involved in a romantic relationship with Petraeus.

[Review: Paula Broadwell’s book on Petraeus]

On Oct. 26, the Tampa agents arrived at the CIA and began an interview in the director’s office on the seventh floor. The agents wanted to talk to him about Broadwell and Kelley.

Petraeus admitted the affair with Broadwell and told the agents he had lost his moral compass. He said the relationship began after he left the military.

In the interview at Langley, the FBI agents also asked the CIA director about secret PowerPoint briefings on the Afghan war that were in Broadwell’s possession. They also asked if he had provided classified information to Broadwell or facilitated her obtaining it. He denied ever doing that — a statement that later led some in the Justice Department to argue that he should be charged with lying to the FBI when it emerged that she had more sensitive material.

The interview ended after about an hour, with Petraeus realizing that his career was in jeopardy because of the affair.

He spoke with Joyce and asked if there was any way to quietly resolve the issue and avoid a scandal, according to former and current U.S. officials.

The FBI’s position was clear: The agents were going to follow the facts.

James R. Clapper Jr., the director of national intelligence, learned about the investigation. Petraeus offered his resignation but said he hoped there was a way he could remain CIA director.

On Nov. 9, 2012, he stepped down.

An expanding investigation

What had first appeared to be a cyber-stalking case was rapidly expanding into a national security leak investigation as FBI agents copied the hard drive of Broadwell’s computer and found secret documents. On Nov. 12, with her consent, agents searched Broadwell’s house.

In addition, investigators discovered more than 100 photographs she had taken of highly classified information from eight bound notebooks Petraeus had kept while commander of U.S. and coalition forces in Afghanistan.

They also found other classified material in her possession linked to a period in 2003 when she served on a Joint Terrorism Task Force in Denver.

It eventually became clear to the FBI that Petraeus had given those journals to Broadwell as part of her research for her book; the FBI seized the journals in April 2013 after searching Petraeus’s house in Virginia.

Broadwell also recorded a conversation in which Petraeus told her that the journals contained classified information, a statement the FBI would attempt to use against him.

But there was disagreement inside the Justice Department and the FBI about whether Broadwell should be charged, with some arguing that she enjoyed protection as a journalist.

In June 2013, following harsh criticism of leak investigations targeting the news media, Holder said he would not indict any journalists for doing their jobs. Broadwell had media credentials while researching in Afghanistan, and she had written stories and op-eds in newspapers and policy journals.

Her lawyers, including Robert F. Muse, met with prosecutors in May 2014.

“We established that Paula Broadwell was a fully credentialed member of the media and entitled to all the protections under the First Amendment and DOJ policy,” Muse said. “Ultimately the government’s decision is consistent with what the attorney general told Congress and what President Obama stated: Namely, members of the media would not be prosecuted for doing their job.”

He said he had no further comment on the case.

Former and current Justice Department officials said prosecutors did give her media status.

The cyber-stalking investigation that began in Tampa was closed at the end of 2012, but prosecutors in Charlotte, where Broadwell lived, continued to examine whether Petraeus had leaked classified information and whether he had lied to the FBI.

In July 2013, Petraeus’s lawyers met with prosecutors in North Carolina who told them that there was an issue in the case involving classified information and that they would need security clearances before it could be fully discussed. Also in attendance at that meeting in Charlotte was Richard Scott, a lawyer in the counterespionage section of the Justice Department’s national security division in Washington — and Petraeus’s lawyers regarded his presence as an ominous sign.

Months of silence ensued, however, until February 2014, when the lawyers were invited back to Charlotte for a meeting with prosecutors, who planned to lay out their case — the same presentation Holder and Comey had listened to.

The prosecutors emphasized that they were pursuing felonies, not misdemeanors, including a conspiracy charge. The presentation focused in particular on the contents of the eight notebooks. Lawyers for the general learned for the first time that the Justice Department was threatening to charge Petraeus with three felonies, including “gathering, transmitting or losing defense information” under the Espionage Act.

A conviction could have sent Petraeus to prison and cost him his pension.

[Petraeus: The Islamic State isn’t our biggest problem in Iraq]

Slowly reaching a deal

In late April, at a meeting in Washington with prosecutors, Petraeus himself listened to the same presentation of the government’s case.

His legal team later rejected any possibility of pleading guilty to felony offenses. In July 2014 in Charlotte, Petraeus’s lawyers told prosecutors they couldn’t show that he intended to disclose classified information and pointed to Broadwell’s book, which contained none and had been personally vetted by the general. And they brought up an array of classified material that had appeared in other books and articles, including some written by Cabinet members, and had not led to prosecutions. That showed, they said, that some of the material Broadwell had obtained from Petraeus was already in the public domain.

They also said his statements to the FBI weren’t material to the investigation and didn’t impede it. And the lawyers pointed to Justice Department guidelines, which say it is not policy to charge “in situations in which a suspect, during an investigation, merely denies guilt in response to questioning by the government.”

In early February 2015, lawyers for Petraeus and the government met once again at the Bicentennial Building in the District. James Melendres, a prosecutor with the national security division, offered a deal.

For this to go away, he said, Petraeus would have to plead guilty to lying to the FBI and mishandling classified information, a misdemeanor. In the statement of facts that would accompany the plea agreement, prosecutors also said they would want to reference a message Petraeus sent to the CIA workforce in 2012 after John Kiriakou, a former agency officer, was convicted of leaking classified information.

“Oaths do matter, and there are indeed consequences for those who believe they are above the laws that protect our fellow officers and enable American intelligence agencies to operate with the requisite degree of secrecy,” Petraeus had said.

Petraeus’s lawyer, David E. Kendall, declined to comment. But another person familiar with the meeting said he described the lying charge as “a nonstarter.” The Kiriakou reference was also off the table, he said.

Scott, the national security division prosecutor, threatened to call off the talks if Kendall insisted on a no-contest plea. On this, Kendall relented.

The end came about a week later when the sides hammered out the agreement on a misdemeanor guilty plea. Petraeus, in a statement of facts, would admit that his statements to the FBI “were false.” The agreed fine was $40,000, and he accepted probation for two years.

[The statement of facts in the Petraeus case]

On April 23, 2015, Petraeus pleaded guilty in Charlotte. The judge upped the fine to $100,000.

A former senior Justice Department official said it was the “cleanest” possible outcome for both sides.

Holder, who was planning to step down and didn’t want to leave the case for the next attorney general, approved the settlement. He declined to comment. But he offered this explanation for his decision at a media event last year, when asked if there was a double standard that allowed Petraeus to plead to a misdemeanor when his department had zealously pursued others for similar alleged crimes.

“There were factors that made the resolution of the case appropriate,” Holder said. “There were some unique things that existed in that case that would have made the prosecution at the felony level and a conviction at the felony level very, very, very problematic.”

Julie Tate, Craig Whitlock and Ellen Nakashima contributed to this report.

Read more:

David Petraeus apologizes again for affair, lays out vision for Iraq and Syria

Petraeus: The Islamic State isn’t our biggest problem in Iraq

Petraeus pleads guilty, will face probation

Gen. David Petraeus: From hero to zero