CIA Director David Petraeus has resigned as head of the main US spy agency, saying he had engaged in an extramarital affair and acknowledging that he "showed extremely poor judgement".

In a letter to the CIA workforce, Mr Petraeus, 60, said that he met US President Barack Obama at the White House yesterday 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."

Mr Obama, who was re-elected to a second term on Tuesday, said in a statement that he accepted Mr Petraeus' resignation, praising him for his work at the CIA and for leading US troops in Iraq and Afghanistan.

The sudden and dramatic turn of events appeared to end the public career of a widely admired man, who played a key role in the Iraq war, led the US Central Command and commanded US and NATO troops in Afghanistan.

Officials say revelations about the affair that led to Mr Petraeus' resignation were discovered in the course of an FBI investigation.

The officials, briefed on what led to the CIA director's sudden resignation, spoke to the Associated Press on condition of anonymity because they were not authorised to publicly discuss the matter.

It was unclear what the FBI was investigating or when it discovered Mr Petraeus’ affair.

Mr Petraeus' name had once circulated speculatively as a possible Republican presidential nominee before Mr Obama tapped him as CIA chief. Before taking the CIA post, he retired as an army general after nearly four decades of military service.

Mr Petraeus had led the CIA for only 14 months. His sudden departure threatened to usher in a period of instability at the agency, which is grappling with a levelling off in its budget after a decade of steady increases.

The agency is also fending off questions about its performance before and after the attack that led to the death of US Ambassador Chris Stevens in Benghazi, Libya.

US officials insisted that the CIA's handling of the Benghazi incident had nothing to do with Mr Petraeus' decision to resign.

Mr Petraeus had recently travelled to Libya and the Middle East, and had been scheduled to testify about the Benghazi events next week behind closed doors to the House and Senate intelligence committees.

In his statement, Mr Obama said, "I am completely confident that the CIA will continue to thrive and carry out its essential mission."

However, there is no indication that he broke any agency rule in connection with his admitted affair, sources familiar with the matter said.

The CIA has no broad rule banning officials from engaging in extramarital affairs, though if discovered, liaisons by CIA personnel with suspected foreign agents would pose security problems for a US spy.

Mr Obama, who accepted Mr Petraeus' resignation in a phone call with him this afternoon, said that Michael Morell, the agency's long-time deputy director, would serve as acting CIA chief.

Mr 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.

He is a leading candidate to be Mr Petraeus' permanent successor, sources said.

Other possible candidates being discussed on Capitol Hill include John Brennan, Mr Obama's chief counter-terrorism adviser; Mr Obama's national security adviser Thomas Donilon; and former congresswoman Jane Harman, who chaired the House intelligence committee.

Mr Petraeus' resignation also adds a new vacancy on Mr Obama's national security team. Secretary of State Hillary Clinton has said she will leave after Mr Obama's first term, and Defence Secretary Leon Panetta is widely expected to leave as well.

Mr Petraeus' wife, Holly, has been an advocate for US veterans and head of the Office of Servicemember Affairs at the US Consumer Financial Protection Bureau.

Last month, Mr Petraeus and Holly appeared together at a reception at the Canadian Embassy in Washington to celebrate the premiere of the Ben Affleck film "Argo".

The film chronicles a successful operation in which the CIA and Canadian diplomats smuggled a group of US officials out of Tehran during the 1979-80 US Embassy hostage crisis.