Baghdad: 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 war that began with the invasion to topple Saddam Hussein.

There are a little more than 4,000 US soldiers in Iraq, but they will depart in the coming days, at which point almost no more American troops will remain in a country where there were once nearly 170,000 personnel 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 2003 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 staged near Baghdad's airport.

"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," said Panetta.

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." General Lloyd Austin, the commander United States Forces

Iraq (USF-I), cased the colours at the ceremony, rolling the USF-I flag around its pole and covering it with a camouflage bag.

He noted that "eight years, eight months and 26 days ago, as the assistant division commander for manoeuvre for the 3rd Infantry Division, I gave the order for the lead elements of the division to cross the border" into Iraq.

"I was here when we originally secured this airfield," he said.

The ceremony was also attended by US ambassador to Iraq James Jeffrey, General Martin Dempsey, the chairman of the Joint Chiefs of Staff, and General James Mattis, the head of the US Central Command, and about 160 US soldiers.

Iraq was represented by military chief of staff Lieutenant General Babaker Zebari and defence ministry spokesman Major General Mohammed al-Askari.

"For over 20 years, Iraq has been a defining part of our professional and personal lives," said Dempsey.

"We will remember you and those that have gone before -- what you risked, what you learned, how you sacrificed... and the fallen comrades for whom we all still grieve."

The ceremony comes a day after hundreds of people in Fallujah marked the impending departure of American forces by burning US flags and shouting slogans in support of the "resistance."

Fallujah, a city of about half a million people west of Baghdad, remains deeply scarred by two American military offensives in 2004, the latter of which is considered one of the fiercest for the United States since Vietnam.

Obama's predecessor George W. Bush ordered the invasion of Iraq in 2003, arguing Saddam was endangering the world with weapons of mass destruction programmes.

Saddam was ousted from power and later executed, but such arms were never found.