In a letter to the CIA workforce, Petraeus, 60, said he met President Barack Obama at the White House on Thursday (local time) 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 organization such as ours."
Obama, who was re-elected to a second term this wek, said in a statement he accepted Petraeus' resignation, praising him for his work at the CIA and for leading US troops in Iraq and Afghanistan.
Petraeus' departure, sudden and unexpected, appeared to end the public career of a man who played a key role in the Iraq war, led the US Central Command and commanded US and NATO troops in Afghanistan.
It also threatened to usher in a period of instability at the CIA, which is grappling with a plateau in its budget after a decade of steady increases and is fending off questions about its performance before and after the attack that led to the death of US Ambassador Chris Stevens in Benghazi, Libya.
Intelligence sources said that Michael Morell, the agency's long-time deputy director, would serve as acting CIA chief for the immediate future.
Morell, who is well respected at both the White House and on Capitol Hill, had previously served as acting director following the departure of former CIA chief Leon Panetta.
Petraeus' wife, Holly, has been an advocate for US veterans and head of the Office of Servicemember Affairs at the U.S. Consumer Financial Protection Bureau.