BAGHDAD - After nearly nine years of war, the loss of more than 100,000 lives, and hundreds of billions of dollars spent, the U.S. military mission in Iraq has formally ended.

But violence continues to roil Iraq, and its political destiny is far from certain.

Defense Secretary Leon E. Panetta and other top U.S. officials conducted a low-key ceremony on a military base at the Baghdad airport Thursday, furling the flag to signal the official conclusion of one of the most divisive wars in American history.

Panetta did not address the origins of the war or Iraq's continuing troubles. Instead, he paid tribute to the sacrifices of U.S. troops, nearly 4,500 of whom were killed and 32,200 wounded since President George W. Bush ordered the March 2003 invasion that toppled Saddam Hussein.

"To be sure, the cost was high - in blood and treasure for the United States and for the Iraqi people," Panetta told about 200 troops and a few Iraqi officials during a 45-minute ceremony. "But those lives were not lost in vain - they gave birth to an independent, free, and sovereign Iraq."

Only two U.S. bases and about 4,000 troops remained in Iraq as of Thursday, the rear guard of a force that was more than 170,000 strong at the height of the war and that once controlled hundreds of bases. The last of them will depart Iraq by the weekend, officials said. About 200 U.S. military personnel will stay in Baghdad to administer arms sales and other limited military exchanges.

After more than eight years of security efforts, employees of the U.S. Embassy in Baghdad say they still find it too dangerous to work outside the campuslike Green Zone, hidden behind a series of towering walls.

But there is no sanctuary from the sectarian divisions. The Shiite-dominated government of Prime Minister Nouri al-Maliki is riddled with corruption, divided, and often dysfunctional. Resentment over their political marginalization still simmers among the Sunni minority that ran Iraq in Hussein's day.

The government also faces problems with private Shiite militias, some with close ties to Iran. Muqtada al-Sadr, the virulently anti-American cleric whose militiamen have fought and killed U.S. troops, controls the Promised Day Brigade in open defiance of the new constitution. Sadr's party holds 40 seats in parliament.

The violence also goes on. By some estimates, an average of 30 bombings and other attacks take place each week, and about 10 deaths a day. That death toll is roughly 20 percent of the daily toll during the worst days of sectarian warfare in 2006. More than 100,000 Iraqi civilians have been killed, according to Iraq Body Count, a website tracking the war. About 12 percent died at the hands of American forces, and the rest in extremist attacks, sectarian violence, and extrajudicial executions.

The security of civilians is now the responsibility of Iraqi soldiers and police, now visible on virtually every major street in Baghdad.

With Americans gone, it is up to men such as Cpl. Hatim Abdul Kareem to help control the endemic violence. He has his doubts. A Shiite, he lost a cousin to a sectarian killing. He fears more bloodletting after U.S. troops leave.

"After the Americans are gone, there will be war in the streets," he said. "This is not just me saying this. Other soldiers are saying this. My family, my friends - they're all saying the violence will get worse."

For the White House, the departure fulfills President Obama's pledge to end U.S. military involvement in Iraq, a move polls suggest is supported by many Americans. As a candidate, Obama once called the Iraq conflict a "dumb war." But the continuing violence and fears that Iran is usurping American influence in postwar Iraq - a scenario that critics forecast before Bush launched the invasion - have made the administration sensitive to political claims that it has "lost" Iraq.

As a result, the administration made great efforts to keep a significant troop presence in Iraq, conceding to the inevitability of full withdrawal only after the Iraqis refused to grant U.S. troops legal immunity to prosecution.

Even so, U.S. involvement in Iraq is not over. Earlier this week, Maliki met with Obama in Washington, where they pledged to proceed with a new, vaguely defined "equal partnership" between the two nations.

Iraq has requested more U.S. military training. U.S. military and civilian trainers at the U.S. Embassy train Iraqis to use warplanes and tanks bought from the United States. Iraq is now the fourth-biggest buyer of U.S. military hardware in the Mideast.

There was no mention during the ceremony of Hussein's pursuit of weapons of mass destruction or his ties to al-Qaeda - the Bush administration's largely discredited reasons for invading Iraq in 2003. Hussein had no such weapons. And he despised the Islamic extremism of al-Qaeda, whose members flowed into Iraq only during the chaos after the American invasion.

In the ceremony, Gen. Ike Austin, commander of U.S. forces in Iraq, furled his flag, terminating his command.

"Let me be clear," Panetta said. "Iraq will be tested in the days ahead - by terrorism and by those who would seek to divide."

But the Obama administration has adopted its own version of the Bush administration assertion that the conflict was worth the cost because it helped free Iraq from Hussein.

Panetta and Austin were joined by Ambassador James Jeffrey and Army Gen. Martin Dempsey, chairman of the Joint Chiefs of Staff. Dempsey was the commander of the First Armored Division in 2006 when Sunni-Shiite violence erupted in and around Baghdad, leading to the toughest fighting of the war.

"We paid a great price here," Dempsey said. "And it was a price worth paying."