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