Media playback is unsupported on your device Media caption US Defences Secretary, Leon Panetta: "To all of the men and women in uniform today your nation is deeply indebted to you."

The flag of American forces in Iraq has been lowered in Baghdad, bringing nearly nine years of US military operations in Iraq to a formal end.

The US Defence Secretary, Leon Panetta, told troops the mission had been worth the cost in blood and dollars.

He said the years of war in Iraq had yielded to an era of opportunity in which the US was a committed partner.

Only about 4,000 US soldiers now remain in Iraq, but they are due to leave in the next two weeks.

At the peak of the operation, US forces there numbered 170,000.

Analysis For 40 years, Iraq has been one of the most damaged countries on earth. The American-led invasion and overthrow of Saddam led to a savage civil war which is still not finished. The United States leaves behind a country embittered by the occupation. And yet today, as the Americans pull down their flag and leave, some Iraqis hope that their country's luck may be turning. If Iraq becomes wealthy, if it can stay more or less democratic, if it can finally bring terrorism to an end, then the 40 years of horror may be over. Its people deserve a little good luck at last. Read John Simpson's analysis in full

The symbolic ceremony in Baghdad officially "cased" (retired) the US forces flag, according to army tradition.

It will now be taken back to the USA.

Mr Panetta told US soldiers they could leave Iraq with great pride.

"After a lot of blood spilled by Iraqis and Americans, the mission of an Iraq that could govern and secure itself has become real," he said.

Iraqi Deputy Prime Minister Hussain al-Shahristani said Iraqis were glad the US troops were leaving.

"They have been difficult years," he told the BBC.

"We have had some successes together. We had some failures. We have some mishaps.

"I think we are all happy that the American soldiers are returning home safely to their families and we are also confident that the Iraqi people and their armed forces, police, are in a position now to take care of their own security."

Some 4,500 US soldiers and more than 100,000 Iraqis have died in the war.

The conflict, launched by the Bush administration in March 2003, soon became hugely unpopular as claims that Saddam Hussein was hiding weapons of mass destruction and supporting al-Qaeda militants turned out to be untrue.

The war has cost the US some $1tr.

Republicans have criticised the pullout citing concerns over Iraq's stability, but a recent poll by the Pew Research Centre found that 75% of Americans backed the troop withdrawal.

'Moment of success'

President Barack Obama, who came to office pledging to bring troops home, said on Wednesday that the US left behind a "sovereign, stable and self-reliant Iraq".

In a speech in North Carolina to troops who have just returned, Mr Obama hailed the "extraordinary achievement" of the military and said they were leaving with "heads held high".

"Everything that American troops have done in Iraq, all the fighting and dying, bleeding and building, training and partnering, has led us to this moment of success," he said.

Media playback is unsupported on your device Media caption Barack Obama: "You have shown why the US military is the finest fighting force in the history of the world"

"The war in Iraq will soon belong to history, and your service belongs to the ages."

He said the war had been "a source of great controversy" but that they had helped to build "a sovereign, stable and self-reliant Iraq, with a representative government that was elected by its people".

Mr Obama announced in October that all US troops would leave Iraq by the end of 2011, a date previously agreed by former President George W Bush in 2008.

Some 1.5 million Americans have served in Iraq since the US invasion in 2003. In addition to those who died, nearly 30,000 have been wounded.

Troop numbers peaked during the height of the so-called surge strategy in 2007, but the last combat troops left Iraq in August last year.

A small contingent of some 200 soldiers will remain in Iraq as advisers, while some 15,000 US personnel are now based at the US embassy in Baghdad - by far the world's largest.

Timeline - US troops in Iraq March 2003 - Operation Iraqi Freedom begins with a "shock and awe" assault on Baghdad, which falls in under a month

May 2003 - President George Bush declares "mission accomplished"

Dec 2003 - Saddam Hussein captured in a bunker south of Tikrit

April 2004 - Photos emerge showing abuse of detainees at Abu Ghraib prison

2005 - Suicide attacks in Iraq hit all-time high as insurgency spreads

January 2007 - US troop "surge" begins, leading to a drop in violence by 2008

August 2010 - Last US combat troops leave Iraq Timeline: US troops in Iraq

'Ruin and mess'

Some Iraqis have said they fear the consequences of being left to manage their own security.

Baghdad trader Malik Abed said he was grateful to the Americans for ridding Iraq of Saddam Hussein, but added: "I think now we are going to be in trouble. Maybe the terrorists will start attacking us again."

But in the city of Falluja, a former insurgent stronghold which was the scene of major US offensives in 2004, people burned US flags on Wednesday in celebration at the withdrawal.

"No-one trusted their promises, but they said when they came to Iraq they would bring security, stability and would build our country," Ahmed Aied, a grocer, told Reuters news agency.

"Now they are walking out, leaving behind killings, ruin and mess."

Concerns have also been voiced in Washington that Iraq lacks robust political structures or an ability to defend its borders.

There are also fears that Iraq could be plunged back into sectarian bloodletting, or be unduly influenced by Iran.