BAGHDAD (AFP) - US forces formally marked the end of their mission in Iraq with a low-key ceremony near Baghdad on Thursday, after nearly nine years of divisive war that began with the invasion to oust Saddam Hussein. The "casing of the colours" near the airport, the first site the US occupied in Baghdad in 2003, comes with around 4,000 US soldiers still in Iraq, all of whom will depart in the coming days. After that, all that will remain are 157 soldiers under the authority of the American embassy in Iraq, a country where there were once nearly 170,000 troops on more than 500 bases. The withdrawal ends a war that left tens of thousands of Iraqis and nearly 4,500 American soldiers dead, many more wounded, and 1.75 million Iraqis displaced, after the US-led invasion unleashed brutal sectarian fighting. "After a lot of blood spilled by Iraqis and Americans, the mission of an Iraq that could govern itself has become real," US Defence Secretary Leon Panetta said at the symbolic flag-lowering ceremony. "Iraq will be tested in the days ahead -- by terrorism and by those who would seek to divide it, by economic and social issues, by the demands of democracy itself," Panetta said. But the US "will stand by the Iraqi people as they navigate those challenges". "This is a time for Iraq to look forward. This is an opportunity for Iraq to forge ahead on a path to security and prosperity," he said. "And we undertake this transition today reminding Iraq that it has in the United States a committed friend and partner. We owe it to all of the lives that were sacrificed in this war not to fail." Panetta described the US withdrawal as "nothing short of miraculous" and "one of the most complex logistical undertakings in US military history." It brings to an end nearly nine years of US military involvement in Iraq, beginning with a "shock and awe" campaign in 2003 that many in Washington believed would see US forces conclude their mission in Iraq within months. But key decisions taken at the time have since been widely criticised as fuelling what became a bloody Sunni Arab insurgency, in particular dissolving the Iraqi army and purging the civil service of all members of Saddam's Baath Party, including lower-echelon members. The insurgency eventually sparked widespread communal bloodshed, particularly after the February 2006 bombing of a Shiite shrine in the predominantly Sunni city of Samarra by Al-Qaeda. More than 100,000 Iraqis have been reported killed since the invasion, according to British NGO Iraq Body Count. The bloodshed was only quelled when then-president George W. Bush ordered a "surge" of American troops to Iraq, and Sunni tribal militias sided with US forces against Al-Qaeda. Attacks remain common, but violence in Iraq has declined significantly since its peak in 2006 and 2007 "A lot of us feel like we haven't done a whole lot of good," said Sergeant Teddy Loftis, one of the 160-odd soldiers seated for the ceremony. "We accomplished our mission, but we don't feel like we've won. "There's still terrorists here, there's still Al-Qaeda here. "(We are) happy to go home, but a little disappointed in how it's ending." The pullout, enshrined in a 2008 pact, is the latest stage in the changing US role in Iraq, from 2003-2004 when American officials ran the country to 2009 when the United Nations mandate ended.