At a ceremony in Kabul Petraeus passed the baton to John Allen, a former subordinate who made his name in Iraq by striking tribal alliances considered integral in reversing Al-Qaeda`s momentum after years of appalling violence.
Allen, who was promoted to a four-star general before the handover, becomes the first Marine to serve as head of the US-led war effort in Afghanistan.
Petraeus oversaw a surge of tens of thousands of troops into Afghanistan in a last-ditch bid to reverse a nearly 10-year Taliban insurgency and although he has claimed some progress, violence remains at record highs.
He is leaving to head up the CIA, after a week in which Afghan President Hamid Karzai saw his younger brother and a key aide assassinated at their homes, and as NATO began transitioning areas of the country to Afghan control.
Washington has now started to draw down troop numbers under a controversial timetable, which Petraeus has admitted he did not recommend, that has attracted widespread criticism for being too fast to hold onto tentative gains.
Flanked by the top US military officer, Admiral Mike Mullen, top US Marine General James Mattis, NATO commander General Wolf Langheld and his replacement, Petraeus handed over his role in a passing of flags.
Celebrated in Washington for turning around the war in Iraq, his legacy in Afghanistan, however, has been less clear.
Petraeus warned of a tough fight ahead, referring to Taliban and Al-Qaeda hideouts in neighbouring Pakistan, which are subject to a covert and escalating CIA drone war.
"There is nothing easy about such a fight, especially when the enemy can exploit sanctuaries outside the country," he said.
On Monday, four NATO soldiers were killed in bomb attacks in the south, and eastern Afghanistan, which borders Pakistan, the military announced.
The outgoing commander praised the nearly 150,000 foreign troops serving in Afghanistan and credited his successor Allen as "the right man for the job".
"You and our Afghan partners have arrested the momentum from the enemies of the new Afghanistan... You have taken away from the insurgency important areas in the former Taliban heartland," he said.
But UN statistics released last week show that 1,462 civilians died in the first six months of 2011, an increase of 15 percent, and putting this year on track to be the deadliest in a decade.
Last Tuesday`s killing of Ahmed Wali Karzai, probably the most powerful man in southern Afghanistan and younger half-brother of the Afghan president, has also been considered a threat to US gains against the Taliban in Kandahar.
Last night`s killing of one of his closest allies, Jan Mohammad Khan, a former governor of southern Uruzgan province, in a raid on his Kabul home has been seen as another loss for the president.
The Taliban claimed responsibility for both killings.
Petraeus took charge in Afghanistan in extraordinary circumstances after US President Barack Obama sacked his predecessor, Stanley McChrystal, over scathing remarks to Rolling Stone magazine about the US administration.
He is leaving at a time of great change in US personnel on Afghanistan. The US ambassador to Kabul, Karl Eikenberry, is being replaced by another former Iraq veteran, Ryan Crocker, and Petraeus` predecessor at the CIA, Leon Panetta, has recently replaced Robert Gates as US defence secretary.
On the job, Petraeus oversaw his trademark counter-insurgency teachings which were deemed to have been so successful in Iraq, backed by a buildup of more than 30,000 extra American troops, now due to go home by the end of 2012.
But although the military is seen to have inflicted heavy casualties among the Taliban, particularly in the south, it has struggled to harness a tribal "awakening" of the type so instrumental in Iraq.
Efforts to broker peace talks with insurgent leaders or woo Taliban fighters have yet to produce a major breakthrough.
Afghan Defence minister Abdul Rahim Wardak, at the ceremony, praised Petraeus` contribution to Afghanistan as "unprecedented" but warned against a hasty drawdown of foreign forces.
The killings of Khan and Wali Karzai, as well as a Taliban attack on the Intercontinental hotel in Kabul last month that left 21 dead, have fuelled doubts about the readiness of Afghans to manage national security. (*)
Editor: Kunto Wibisono
Copyright © ANTARA 2011