Petraeus issued a statement acknowledging the affair after President Barack Obama accepted his resignation and it was announced by the CIA. He had offered to resign Thursday when he informed Obama about the affair, White House officials said.
Government officials said that the FBI had investigated whether a computer used by Petraeus had been compromised. In the course of that inquiry, federal investigators discovered the relationship, officials said.
Senior members of Congress were alerted to Petraeus' impending resignation by intelligence officials about six hours before the CIA announced his resignation. One congressional official who was briefed on the matter said that Petraeus had been encouraged "to get out in front of the issue" and resign, and that he agreed.
As for how the affair came to light, the congressional official said that "it was portrayed to us that the FBI was investigating something else and came upon him. My impression is that the FBI stumbled across this."
The official said they were not told the name of the woman or any other details about the FBI investigation.
The FBI did not inform the Senate and House Intelligence Committees about the investigation before this week, according to congressional officials, who noted that by law the panels - and especially their chairmen and ranking members - are supposed to be told about significant developments in the intelligence arena. Sen. Dianne Feinstein, D-Calif., the chairman of the committee, plans to pursue the question of why the committee was not told, one official said.
The revelation of a secret inquiry into the head of the nation's premier spy agency raised urgent questions about Petraeus' tenure and the decision by Obama to elevate him last year to head the CIA after leading the country's war effort in Afghanistan. White House officials said they did not know about the affair until this week, when Petraeus informed them.
"After being married for over 37 years, I showed extremely poor judgment by engaging in an extramarital affair," Petraeus said in his statement, expressing regret for his abrupt departure. "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."
Petraeus' admission and resignation represent a remarkable fall from grace for one of the most prominent figures in America's modern military and intelligence community, a commander who helped guide the nation's wartime activities in the decade after the Sept. 11, 2001, attacks and was credited with turning around the failing war effort in Iraq.
Petraeus almost single-handedly forced a profound evolution in the country's military thinking and doctrine with his philosophy of counterinsurgency, focused more on protecting the civilian population than on killing enemies. More than most of his flag officer peers, he understood how to navigate Washington politics and media, helping him rise through the ranks and obtain resources he needed, although fellow Army leaders often resented what they saw as a grasping careerism.
"To an important degree, a generation of officers tried to pattern themselves after Petraeus," said Stephen Biddle, a military scholar at George Washington University who advised Petraeus at times. "He was controversial; a lot of people didn't like him. But everybody looked at him as the model of what a modern general was to be."
White House officials say they were informed Wednesday night that Petraeus was considering resigning because of an extramarital affair. Intelligence officials notified the president's national security staff. Obama was at the time on his way back to Washington from Chicago, where he had gone to receive Tuesday's election returns.
On Thursday morning, just before a staff meeting at the White House, Obama was told.
"He was surprised, and he was disappointed," one senior administration official said. "You don't expect to hear that the Thursday after you were re-elected."
The president closeted himself in the White House all day on Thursday, getting back to his old routine after months on the campaign trail. That afternoon, Petraeus came in to see him personally and informed the president that he strongly believed he had to resign.
Obama did not accept his resignation right away.
"He told him, 'I'll think about it overnight,"' the administration official said.
After months on the road, a disclosure of a career-killing extramarital affair from his larger-than-life CIA director was the last thing that Obama was expecting, the official said.
The president, officials said, did not want Petraeus to leave. But he ultimately decided that he would not lean heavily on him to stay. On Friday, he called.
Petraeus and accepted the resignation, "agreeing with Petraeus' judgment that he couldn't continue to lead the agency," a White House official said.
The White House had hoped to keep the news under wraps until after the daily briefing for the media, but as it was reported on MSNBC, reporters checking their email confronted Jay Carney, the press secretary, who tried to duck the questions.
"I think I'll let Gen. Petraeus address this," Carney said.
Shortly after the news broke, Obama released a statement praising Petraeus for his "extraordinary service" to the country and expressing support for him and his wife, Holly.
"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."
A favourite of President George W. Bush and once the subject of intense speculation about his future as a possible presidential candidate, Petraeus, 60, managed the awkward move from a Republican administration to a Democratic one. He was one of the most telegenic faces of the military during his tenure, testifying frequently in Congress about the country's difficult battles overseas.
Petraeus clashed with Obama in 2008 during a campaign visit to Iraq, having what David Plouffe, his campaign manager, called in his book a "healthy debate" over troop levels in the country.
But the president's decision to tap Petraeus to command the war in Afghanistan and later picking him to lead the CIA, effectively ended lingering concerns among Obama's political advisers that the popular general might challenge his commander in chief during the election that just ended.
Petraeus has been married for 38 years and has two children. His wife serves as the assistant director of the Office of Servicemember Affairs at the Consumer Financial Protection Bureau. Petraeus' resignation and the circumstances surrounding it stunned military officers who have served alongside him in war zones over the past two decades.
"It was a punch in the gut for those of us who know him," said Col. Michael J. Meese, a professor at West Point who has known Petraeus for a decade and served as one of his top aides in Bosnia, Iraq and Afghanistan.
The resignation 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."
In his statement, Petraeus did not provide any details about his behavior, 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.
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. In recent months, Petraeus was seen as a credible part of the administration's response to Republican critics of Obama's handling of the killings at the U.S. diplomatic mission in Benghazi, Libya. Petraeus had been scheduled to testify at a closed-door hearing on the subject next week.
In his statement Friday, Obama said that Michael J. Morell, the deputy director of the CIA, would take over once again as acting director. Morell served in that position briefly after Leon E. Panetta moved to the Defense Department last year.
Among the candidates who might replace Petraeus permanently is John Brennan, the president's adviser for homeland security and counterterrorism. Brennan was considered for CIA director before Obama's term began but withdrew among criticism from some of the president's liberal supporters.