In a letter to the CIA workforce, Petraeus, 60, said he met with President Barack Obama at the White House on Thursday and asked "to be allowed, for personal reasons, to resign from my position."
"After being married for 37 years, I showed extremely poor judgment by engaging in an extramarital affair," he wrote. "Such behaviour is unacceptable, both as a husband and as the leader of an organisation such as ours."
Obama, who was re-elected to a second term on Tuesday, said in a statement he had accepted Petraeus' resignation, praising him for his work at the Central Intelligence Agency and for leading US troops in Iraq and Afghanistan.
The woman with whom the former CIA director had the affair is Paula Broadwell, according to a person familiar with the matter. She is an author who wrote a biography of Petraeus titled All In.
Attempts to reach Broadwell were unsuccessful. There was no answer at the door of her house in the affluent Charlotte, North Carolina, neighbourhood of Dilworth.
Dramatic turn of events
Much about the sudden and dramatic turn of events remained unknown Friday evening, including how long the affair had gone on and what prompted Petraeus to resign now, just days after the 2012 presidential election.
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 US 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.
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 favourite."
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 Defence 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." – Reuters