The former army commander, who led US forces in Afghanistan and Iraq before taking charge of America's top intelligence agency, said he had shown "extremely poor judgment" in being unfaithful to his wife of 37 years.
General Petraeus submitted his resignation to Barack Obama on Thursday, one day after the commander-in-chief returned to Washington following the president's election victory. It was accepted yesterday.
"After being married for over 37 years, I showed extremely poor judgment by engaging in an extramarital affair. Such behaviour is unacceptable, both as a husband and as the leader of an organisation such as ours," he said in a statement.
General Petraeus is married to Holly Petraeus, a senior official in Mr Obama's Consumer Financial Protection Bureau.
Mr Obama praised the outgoing CIA director "as one of the outstanding general officers of his generation".
The president announced that Michael Morrell, General Petraeus's deputy, would step in as acting director of the agency and that he was "completely confident" that the CIA's work would continue uninterrupted.
The abrupt resignation, thought to have been prompted by an imminent newspaper story, brings an end to the career of a man often touted as a future presidential contender.
The 60-year-old, who is known for his iron discipline and who can still run two miles in under 10 minutes, was credited with leading a turnaround in the US war effort in Iraq under President George W Bush, when he led the 2007 "surge" against the insurgency.
Mr Obama then tapped him to replace General Stanley McChrystal as the leader of international forces in Afghanistan. General Petraeus was made CIA director by Mr Obama in 2011, in what was viewed as a savvy move by the president to sideline a potential candidate for the Republican presidential nomination. (©Daily Telegraph, London)