There were indications, however, that the affair was first uncovered a few months ago during an investigation by the FBI.
A US national security source said the FBI had stumbled across evidence of Petraeus' affair during an apparently unrelated investigation of news leaks.
Petraeus' revelation of the affair appeared to end the public career of a widely admired warrior-scholar who played a key role in the Iraq war, led the US Central Command and commanded U.S. and NATO troops in Afghanistan.
Petraeus' name had circulated speculatively as a possible Republican presidential nominee before Obama tapped him as CIA chief. Before taking the CIA post, he retired as an Army general after nearly four decades of military service.
Petraeus led the CIA for only 14 months. His sudden departure threatened to usher in a period of instability at the spy agency, which is grappling with a leveling off in its budget after a decade of steady increases.
The agency is also fending off questions about its performance before and after the attack that led to the death of US Ambassador Chris Stevens in Benghazi, Libya.
US officials insisted the CIA's handling of the Benghazi incident had nothing to do with Petraeus' decision to resign.
TESTIFY ON CAPITOL HILL
Petraeus recently traveled to Libya and the Middle East, and had been scheduled to testify about the Benghazi events next week behind closed doors to the House and Senate intelligence committees. Now, he will not give that testimony.
There is no indication Petraeus broke any agency rule in connection with his admitted affair, sources familiar with the matter said.
The CIA has no broad rule banning officials from engaging in extramarital affairs though, if discovered, liaisons by CIA personnel with suspected foreign agents would pose security problems for a US agent.
In his statement, Obama said “I am completely confident that the CIA will continue to thrive and carry out its essential mission.”
Obama, who accepted Petraeus' resignation in a phone call with him on Friday afternoon, said Michael Morell, the agency's long-time deputy director, would serve as acting CIA chief.
Morell is a leading candidate to be Petraeus' permanent successor, sources said.
He earned Obama's trust when he frequently briefed the president during planning for the operation to take down Osama bin Laden, a senior administration official said.
“He's respected, a straight shooter, and has great relationships with the White House and Capitol Hill. Not to mention over 30 years of agency experience,” said a former CIA official, who called Morell “the odds-on favorite.”
Other possible candidates being discussed on Capitol Hill include John Brennan, Obama's chief counter-terrorism adviser; Obama's national security adviser Thomas Donilon; and former congresswoman Jane Harman, who chaired the House intelligence committee.
Petraeus' resignation also adds a new vacancy on Obama's national security team. Secretary of State Hillary Clinton has said she will leave after Obama's first term, and Defense Secretary Leon Panetta is widely expected to leave as well.
Petraeus' wife, Holly, has been an advocate for US veterans and head of the Office of Servicemember Affairs at the US Consumer Financial Protection Bureau.
Last month, Petraeus and his wife appeared together at a reception at the Canadian Embassy in Washington to celebrate the premiere of the Ben Affleck film “Argo,” which chronicles a successful operation in which the CIA and Canadian diplomats smuggled a group of US officials out of Tehran during the 1979-81 U.S. Embassy hostage crisis.
At the Pentagon, people who worked closely with Petraeus expressed shock at the revelations and at his resignation.
Sen. Dianne Feinstein, chair of the Senate Intelligence Committee, said in a statement: “I wish President Obama had not accepted this resignation, but I understand and respect the decision.”