The sudden development came just days after President Barack Obama won re-election to a second term. Petraeus, a highly decorated general who had led the wars in Iraq and Afghanistan, had been expected to remain in the president's cabinet.
Instead, Petraeus said in the statement that the president accepted his resignation Friday after he had informed him of his indiscretion a day earlier.
"After being married for over 37 years, I showed extremely poor judgment by engaging in an extramarital affair," Petraeus wrote. "Such behaviour is unacceptable, both as a husband and as the leader of an organization such as ours. This afternoon, the president graciously accepted my resignation."
Obama released a statement praising Petraeus for his "extraordinary service" to the country and saying that Michael J. Morell, the deputy director of the CIA, would take over once again as acting director. He served in that position briefly after Leon E. Panetta left the agency last year.
"By any measure, through his lifetime of service, David Petraeus has made our country safer and stronger," the president said. Without directly addressing the affair, Obama added: "Going forward, my thoughts and prayers are with Dave and Holly Petraeus, who has done so much to help military families through her own work. I wish them the very best at this difficult time."
The development came as a shock to the national security establishment. In a statement, James R. Clapper, the director of national intelligence, called the decision "a loss" to the country.
"Dave's decision to step down represents the loss of one of our nation's most respected public servants." Clapper wrote. "From his long, illustrious Army career to his leadership at the helm of CIA, Dave has redefined what it means to serve and sacrifice for one's country."
By acknowledging an extramarital affair, Petraeus, 60, was confronting a sensitive issue for a spy chief. Intelligence agencies are often concerned about the possibility that agents who engage in such behavior could be blackmailed for information.
In his statement, Petraeus did not provide any details about his behaviour, saying that he asked the president to be allowed "for personal reasons" to resign.
Petraeus praised his colleagues at the CIA's headquarters in Langley, Va., calling them "truly exceptional in every regard" and thanking them for their service to the country. He made it clear that his departure was not how he had envisioned ending a storied career in the military and in intelligence.
"Teddy Roosevelt once observed that life's greatest gift is the opportunity to work hard at work worth doing," he said. "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."
Over the past several years, Petraeus had become one of the most recognizable military officials, serving as the public face of the war effort in Congress and on television.
Under President George W. Bush, Petraeus was credited for helping to develop and put in place the "surge" in troops in Iraq that helped wind down the war in that country. Petraeus was moved to Afghanistan in 2010 after Obama fired Gen. Stanley A. McChrystal over comments he made to a magazine reporter.
In Afghanistan, Petraeus led the push for a similar increase in troops ordered by Obama, but he was unable to replicate the success he had in the Iraq conflict.
Last year, Obama persuaded Petraeus to leave the Army after 37 years to lead the CIA, succeeding Panetta, who moved to the Defense Department.