According to his letter of resignation, Mr Petraeus asked US President Barack Obama on Thursday to allow him to resign, and on Friday the president accepted.
Mr Petraeus said in a statement that he had shown "extremely poor judgment" in having an affair. Mr Petraeus has been married for 37 years to Holly Petraeus, whom he met when he was a cadet at the US Military Academy at West Point, New York state.
The resignation has shocked Washington's intelligence and political communities, representing a sudden end to the public career of the best-known general of the past decade, embodying the military to millions of Americans.
"Such behaviour is unacceptable, both as a husband and as the leader of an organisation such as ours," Mr Petraeus said in his statement.
Mr Obama said the general had provided "extraordinary service to the United States for decades" and had offered a lifetime of service that had "made our country safer and stronger."
He added that the CIA's deputy director Michael Morell will serve as acting director. "I am completely confident that the CIA will continue to thrive and carry out its essential mission," Mr Obama added.
In his statement, Mr Petraeus said to Mr Obama: " Teddy Roosevelt once observed that life's greatest gift is the opportunity to work hard at work worth doing. I will always treasure my opportunity to have done that with you, and I will always regret the circumstances that brought that work with you to an end."
Though Mr Obama made no direct mention of Mr Petraeus's reason for leaving, he offered his thoughts and prayers to the general and his wife, saying Mrs Petraeus has "done so much to help military families through her own work. I wish them the very best at this difficult time".