BAGHDAD - Almost nine years after the first U.S. tanks began massing on the Iraq border, the Pentagon declared an official end to its mission here, closing a troubled conflict that helped reshape U.S. politics and left a bitter legacy of anti-U.S. sentiment across the Muslim world.
As Defense Secretary Leon E. Panetta marked the occasion with a speech in a fortified concrete courtyard at the Baghdad airport, helicopters hovered above, underscoring the challenges facing a country where insurgents continue to attack U.S. soldiers and where militants with al-Qaida still regularly carry out devastating attacks against civilians.
"Let me be clear: Iraq will be tested in the days ahead - by terrorism, and by those who would seek to divide, by economic and social issues, by the demands of democracy itself," Panetta said. "Challenges remain, but the U.S. will be there to stand by the Iraqi people as they navigate those challenges to build a stronger and more prosperous nation."
Those words sounded an uncertain trumpet for a war that was begun in 2003 to rid Iraq of weapons of mass destruction that proved illusory.
For Americans, the ceremony Thursday marked an uneasy moment of closure, with no clear sense of what has been won and lost. As of last Friday, the war had claimed 4,487 U.S. lives, with 32,226 more Americans wounded in action, according to Pentagon statistics.
Although Thursday's ceremony represented the official end of the war, the military still has two bases in Iraq and roughly 4,000 troops, including several hundred who attended the ceremony.
At the height of the war in 2007, there were 505 bases and more than 170,000 troops.
Those troops that remain are still being attacked daily, mainly by artillery or mortar fire on the bases, and roadside bombs aimed at convoys heading south toward Kuwait.
Even after the last two bases are closed and the final U.S. combat troops withdraw from Iraq by Dec. 31, a few hundred military personnel and Pentagon civilians will remain, working within the U.S. Embassy as part of an Office of Security Cooperation to assist in arms sales and training to the Iraqis.
But negotiations could resume next year on whether additional U.S. military personnel can return to assist their Iraqi counterparts further.
Iraq's military has critical weaknesses in a number of areas, from air defenses to basic logistical tasks like moving food and fuel and servicing the armored vehicles it is inheriting from the Americans and the jets it is buying. There are shortfalls in military engineers, artillery and intelligence.
"From a standpoint of being able to defend against an external threat, they have very limited to little capability, quite frankly," Gen. Lloyd J. Austin III, the departing American commander in Iraq, said in an interview after the ceremony.
Although the American withdrawal has removed one central motive for the jihadis who flooded into Iraq after the invasion in 2003, Al Qaeda's Iraqi arm has carried out a number of spectacular bombings over the past year, and some intelligence analysts fear it is in resurgence.
Even in its twilight days, the American military here has suffered humiliating attacks that complicated the handover. In the spring, commanders stopped holding large base-closing ceremonies because insurgents were taking advantage of them to strike at troops.
"We were having ceremonies and announcing it publicly and having a little formal process, but a couple of days before the base was to close, we would start to receive significant indirect fire attacks on the location," said Col. Barry Johnson, a spokesman for the military in Iraq. "We were suffering attacks, so we stopped."
Since then, the closing of bases has been a quiet, closed-door meeting, where American and Iraqi military officials have signed documents that legally give the Iraqis control of the bases, exchanged handshakes and turned over keys.
The chairman of the Joint Chiefs of Staff, Gen. Martin E. Dempsey of the Army, has served two command tours in Iraq since the invasion in 2003, and he noted during the ceremony that the next time he comes to Iraq he will have to be officially invited.
"We will stand with you against terrorists and others that threaten to undo what we have accomplished together," General Dempsey said during the end-of-mission ceremony. "We will work with you to secure our common interests in a more peaceful and prosperous region.".