US Defence Secretary, Leon Panetta says Iraq will be tested in the days ahead by people who seek to divide it.
At a ceremony in Baghdad marking the formal end of US combat operations, Mr Panetta said the challenges to Iraq would include terrorism and the demands of democracy.
But he promised the United States would stand by Iraqis.
Mr Panetta told the troops the mission had been worth the cost in blood and dollars.
Only about 4000 US soldiers now remain in Iraq, but they are due to leave in the next two weeks. US forces there numbered 170,000 at the peak of the operation.
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 4500 US soldiers and more than 100,000 Iraqis died in the war, which began in March 2003.