CIA Director David H. Petraeus resigned Friday and admitted to having an extramarital affair, bringing a shocking end to his brief tenure at the spy agency and highly decorated national security career.

The affair came to light as part of an FBI investigation into a potential security breach involving Petraeus’s e-mails, according to federal law enforcement officials and a former senior intelligence official. The investigation uncovered e-mails describing an affair between Petraeus and Paula Broadwell, a former military officer and co-author of a glowing biography of Petraeus, according to two law enforcement officials who were briefed on the investigation.

Petraeus, a retired four-star Army general who once was seen as a potential presidential candidate, met with President Obama on Thursday and said he intended to step down because of the affair, Obama administration officials said. The president accepted his resignation Friday.

“After being married for over 37 years, I showed extremely poor judgment by engaging in an extramarital affair,” Petraeus said in a statement distributed to the CIA workforce Friday.

“Such behavior is unacceptable, both as a husband and as the leader of an organization such as ours. This afternoon, the president graciously accepted my resignation,” he said.

A senior administration official said the White House learned only Wednesday that Petraeus had a potentially serious problem. The official said that Petraeus telephoned Thomas E. Donilon, the national security adviser, early Thursday and asked to meet with Obama.

The investigation is not expected to result in accusations of criminal wrongdoing by Petraeus or Broadwell, according to the two law enforcement officials, who spoke on the condition that their names be withheld because they were not authorized to speak publicly.

Paul Bresson, an FBI spokesman, refused to comment. Attempts to reach Broadwell were unsuccessful. A CIA spokesman declined to answer questions about the timing of the affair or Petraeus’s decision to disclose it to the White House.

Current and former U.S. military officials said suspicions of infidelities had followed Petraeus for several years.

The sudden departure of Petraeus created turmoil in the administration’s national security team just days after the president’s reelection. That team was expected to see a series of changes in the coming months, but many believed that Petraeus would remain in position.

In a statement, Obama said Petraeus has “provided extraordinary service to the United States for decades,” adding that “through his lifetime of service David Petraeus has made our country safer and stronger.”

The statement did not directly address Petraeus’s reason for resigning, but the president said that his “thoughts and prayers are with Dave and Holly Petraeus, who has done so much to help military families through her own work. I wish them the very best at this difficult time.”

Holly Petraeus is an assistant director of the U.S. Consumer Financial Protection Bureau, where she is charged with advocating on behalf of service members and their families. She and her husband met in 1973 at the U.S. Military Academy at West Point, where her father was superintendent.

Broadwell, who also is married, is a West Point graduate and a research associate at Harvard University. She is the co-author of “All In: The Education of General David Petraeus.” (The book’s co-author was Vernon Loeb, local editor at The Washington Post.)

In earlier interviews, Broad­well described meeting Petraeus in 2006 at Harvard, where she was working on a dissertation about leadership. She said they soon started e-mailing and discussing her research.

In the preface to the book, Broadwell said that after Obama picked Petraeus to lead U.S.
forces in Afghanistan in June 2010, he invited her to Kabul, and she decided to turn her dissertation into a biography. She made repeated trips to Afghanistan to spend time observing Petraeus.

In describing Petraeus in a CBS News interview two months ago, she said: “He, at the end of the day, is human and is challenged by the burdens of command. . . . So, he has this mask of command — you think he’s really confident — but I got to see a more personal side. He’s confident, but he’s also very compassionate about the loss of troops and sacrifices we’re making in Afghanistan.”

Petraeus was scheduled to testify next week on Capitol Hill in hearings on the deaths of four Americans, including the U.S. ambassador and two CIA security officers, in Libya in September.

U.S. officials insisted that the controversy surrounding the attack — and the administration’s shifting accounts of it — played no role in Petraeus’s decision to resign.

Petraeus’s 14-month tenure as CIA director is one of the shortest in agency history.

Michael J. Morell, who served as Petraeus’s deputy at the CIA, will serve as interim director, a position he occupied for several months before Petraeus was sworn in. Morell is seen as a leading candidate to replace Petraeus, but there are others, including Michael G. Vickers, a former CIA paramilitary officer now serving as undersecretary of defense for intelligence.

Petraeus came into the CIA job after a highly decorated Army career that included command of the war efforts in Iraq and Afghanistan, which made him one of the most venerated officers of his generation.

Sen. Dianne Feinstein (D-
Calif.), chairman of the Senate Intelligence Committee, said Friday that she believed Petraeus’s infidelity did not require him to resign.

“I wish President Obama had not accepted this resignation, but I understand and respect the decision,” she said in a statement. She described Petraeus’s resignation as an “enormous loss for our nation’s intelligence community and for our country.”

The nature of Petraeus’s resignation is likely to leave a stain on the polished reputation he cultivated during his 37-year military career. Petraeus was widely credited with helping to reverse the course of the war in Iraq and overhauling the military’s approach to counterinsurgency fighting. Petraeus was later handed command of the war in Afghanistan, where success proved more elusive.

Because of his evident ambition and abundant publicity, some military rivals saw Petraeus as preening and self-aggrandizing. He did little to discourage speculation that he could be a presidential or vice presidential candidate and quietly campaigned for the CIA job when his path to higher military positions was blocked.

At the agency, Petraeus presided over an expansion of the CIA’s Predator drone campaign in Yemen and was recently behind a push to expand the agency’s drone fleet. He was involved in decisions to carry out controversial strikes, including the Predator attacks last year that killed two U.S. citizens: the al-Qaeda figure Anwar al-Awlaki and his teenage son.

Petraeus, who retired from the military last year, is still subject to the Uniform Code of Military Justice, which classifies adultery as a crime.

Practically speaking, however, the odds are extremely low that the military would prosecute a retired officer for having an affair, said Eugene R. Fidell, a prominent military law expert who teaches at Yale University.

“They’re as close to zero as you can get,” Fidell said. “It would have to be a grave matter before the executive branch would prosecute a retiree.”

Petraeus married Holly two months after graduating from West Point. His courtship was seen as audacious because of her father’s rank at the elite military academy. They have two children, Stephen, who became an Army officer, and Anne.

Petraeus has frequently praised his wife in public appearances for her sacrifices and contributions to his career, and he characterized his return to Washington as an opportunity for them to be closer after his years-long assignments overseas.

As Petraeus assumed a low profile when he moved to the CIA, his wife became increasingly visible at the Pentagon as part of her work for a government agency that helps service members manage their finances. In recent months, she has traveled to dozens of military bases around the country to counsel soldiers about predatory lending, student loans and debt.

Max Fisher, Ernesto Londoño, Julie Tate, Joby Warrick, Craig Whitlock and Scott Wilson contributed to this report.