WASHINGTON -- David H. Petraeus, the director of the Central Intelligence Agency, resigned on Friday after issuing a statement saying that he had engaged in an extramarital affair.
The sudden development came just days after President Obama won re-election to a second term. Mr. Petraeus, a highly decorated general who had led the wars in Iraq and Afghanistan, had been expected to remain in the president's administration.
Instead, Mr. Petraeus said in the statement that the president accepted his resignation on 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," Mr. Petraeus wrote. "Such behavior is unacceptable, both as a husband and as the leader of an organization such as ours. This afternoon, the president graciously accepted my resignation."
Mr. Obama released a statement praising Mr. Petraeus for his "extraordinary service" to the country and saying that Michael J. Morell, the deputy director of the C.I.A., 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, Mr. 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." Ms. Petraeus is the assistant director of the Office of Servicemember Affairs at the Consumer Financial Protection Bureau.
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." Mr. Clapper wrote. "From his long, illustrious Army career to his leadership at the helm of C.I.A., Dave has redefined what it means to serve and sacrifice for one's country."
By acknowleding an extramarital affair, Mr. 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, Mr. Petraeus did not provide any details about his behavior, saying that he asked the president to be allowed "for personal reasons" to resign.
Mr. Petraeus praised his colleagues at the C.I.A.'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 last several years, Mr. 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, Mr. 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. Mr. Petraeus was moved to Afghanistan in 2010 after Mr. Obama fired General Stanley H. McChrystal over comments he made to a magazine reporter.
In Afghanistan, Mr. Petraeus led the push for a similar increase in troops ordered by Mr. Obama, but he was unable to replicate the success he had in the Iraq conflict.
Last year, Mr. Obama persuaded Mr. Petraeus to leave the Army after 37 years to lead the C.I.A., succeeding Mr. Panetta, who moved to the Defense Department.
Correction: November 9, 2012, Friday
This article has been revised to reflect the following correction: An earlier version of this article incorrectly stated that David H. Petraeus was expected to remain in President Obama's cabinet. The C.I.A. director is not a cabinet member in the Obama administration.
This article originally appeared in The New York Times.