Former CIA director David Petraeus addresses a University of Southern California event honoring the military in March 2013 in Los Angeles. (Frederic J. Brown/AFP/Getty Images)

David H. Petraeus, the retired general and former CIA director, has reached an agreement with federal prosecutors to plead guilty to a misdemeanor charge for mishandling classified materials, a deal that brings an end to a lengthy investigation that kept him largely out of public view for more than two years.

The deal, if approved by a judge, will spare Petraeus a prison sentence and allow him to avoid a trial that probably would have revealed details of his relationship with his former mistress and biographer, who was the recipient of the classified material in question.

As part of the agreement, Petraeus admitted improperly retaining a number of bound notebooks containing sensitive national security information and giving them to the biographer, Paula Broadwell. According to documents filed Tuesday in federal court in North Carolina, Petraeus initially lied to FBI investigators, telling them in an interview at CIA headquarters that he had never provided Broadwell with classified information.

“The statements were false,” prosecutors said in the court documents. “Defendant David Howell Petraeus then and there knew that he previously shared the black books with his biographer.”

[View: A statement of facts in the Petraeus case]

Prosecutors will not seek prison time for Petraeus. Instead they will ask a judge to impose a probationary period of two years and make him pay a $40,000 fine. It is unclear whether Broadwell will face charges.

Attorneys for Petraeus and Broadwell declined to comment. News of a possible plea deal was first reported by the New York Times.

The handling of the Petraeus investigation has become a subject of political intrigue in Washington, with questions over whether the government would prosecute a retired four-star general who became perhaps the most celebrated military officer of his generation before going on to briefly lead the CIA.

The Justice Department has pursued several high-profile cases involving the unauthorized disclosure of classified information, and a handful of other cases remain open. FBI officials, for instance, have pressed prosecutors to charge James Cartwright, the former vice chairman of the Joint Chiefs of Staff, in connection with the leaking of classified information about the Stuxnet cyberattack against Iran’s nuclear program.

Although Petraeus provided Broadwell with eight of the black books, as they are known, to help her write the general’s memoir, prosecutors said her book, “All In,” did not contain classified information.

[Read: Petraeus’s plea agreement]

According to prosecutors, the black books in Petraeus’s possession dated to his time as top U.S. military commander in Afghanistan. They contained code words, war strategy, the identities of covert officers and other sensitive information. They also outlined deliberative discussions with the National Security Council and President Obama.

In an audiotape of a 2011 conversation recorded by Broadwell and later obtained by investigators, the biographer asked Petraeus about the notebooks and noted that the pair had not yet reviewed them together. “Umm, well, they’re really — I mean they are highly classified, some of them,” he told her.

Court documents indicate that Petraeus gave the books to Broadwell a few weeks later, in August 2011, while she was staying at a private residence in Washington. Several days later, he retrieved them.

The FBI began investigating Petraeus in 2012 after Broadwell sent threatening e-mails to Jill Kelley, a Florida woman who was an associate of Petraeus’s. Kelley, who did not know the identity of the sender, contacted the FBI, which later traced the messages to Broadwell.

In the course of their investigation into Broadwell, the FBI uncovered not only explicit e-mails between her and Petraeus but classified documents, prompting a probe into how she obtained them.

The revelation of Petraeus’s affair led to his resignation as CIA director in November 2012. Two weeks later, he signed documents assuring the agency he no longer had classified documents in his “possession, custody, or control.”

When FBI agents raided Petraeus’s house in Arlington, Va., in April 2013, however, they seized the black books, which they found in an unlocked drawer in his study.

Petraeus had previously turned over classified documents collected during his tenure as head of U.S. forces in Afghanistan to a Pentagon historian but did not hand over the black books, according to court documents.

Petraeus is not the first former CIA director to face charges for mishandling classified information. In December 1996, then-Director John Deutch resigned after agency security officers discovered he had stored highly classified documents on his home computer, which was connected to the Internet.

After a criminal investigation, Deutch agreed to plead guilty to a misdemeanor and pay a $5,000 fine. But before the prosecutors could file the papers in federal court, President Bill Clinton pardoned him on his last day in office.

Petraeus, who routinely basked in the public spotlight during his days as a general, has kept a relatively low profile during the course of the FBI investigation. He now serves as chairman of the KKR Global Institute, a part of the private-equity firm Kohlberg Kravis Roberts, and also spends his time teaching and giving speeches.

On Tuesday, public reaction to Petraeus’s plea deal was muted.

Sen. John McCain (R-Ariz.), chairman of the Armed Services Committee, said the general had “apologized and expressed deep regret for this situation, and I believe it is time to consider this matter closed.”

McCain, in a statement, said he hoped that “Petraeus will continue to provide his outstanding service and leadership to our nation, as he has throughout his distinguished career.”

Julie Tate and Carol D. Leonnig contributed to this report.