BAGHDAD — At a crowded market in the city center here, the flotsam of the war is for sale. Ripped Fuel workout supplement. Ready-to-eat meals, macaroni-and-cheese “Mexican style.” Pistol holsters. Nothing seems off limits to the merchants out for a quick dinar, not even a bottle of prescription pills from a pharmacy in Waco, Tex., probably tossed out by a departing soldier.

The concrete blast walls that shielded the shopping stalls have lately come down. Since then, three explosions have struck the market, killing several people.

“This will be an easy target for car bombs,” said Muhammad Ali, a merchant who lost two brothers during the cruelest times of the conflict. “People will die here.”

After nearly nine years, some 4,500 American fatalities and about $1 trillion, America’s war in Iraq is about to end. Officials marked the finish Thursday with a modest ceremony at the airport days before the last troops traverse the southern highway to Kuwait, going out as they came in, to conclude the United States’ most ambitious and bloodiest military campaign since Vietnam.

Iraqis will be left with a country that is not exactly at war, and not exactly at peace. It has improved in many ways since the 2007 troop “surge,” but it is still a shattered country marred by violence and political dysfunction, a land defined on sectarian lines whose future, for better or worse, is now in the hands of its people.

“It is the end for the Americans only,” Emad Risn, an Iraqi columnist, recently wrote in the pages of Assabah al-Jadeed, a government-funded newspaper. “Nobody knows if the war will end for Iraqis, too.”

Iraq will be on its own to find its place in a region upended by revolutions and to manage its rivalry with Iran, which will look to expand its influence culturally and economically in the power vacuum left by the United States military. While American officials worry about the close political ties between Iraq’s Shiite leadership and Iran, the picture at the grass-roots level is more nuanced. Iraqis complain about shoddy Iranian consumer goods — they frequently mention low-quality yogurts and cheeses — and the menacing role of Iranian-backed militias, which this year killed many American soldiers.

The Iranian rivalry frequently plays out in the Shiite holy city of Najaf, where Iraq’s religious authorities are headquartered. Iran, which like Iraq is majority Shiite, recently installed one of its leading clerics in Najaf, raising worries that Iran is trying to spread its brand of clerical rule to Iraq. Meanwhile, Moktada al-Sadr, the anti-American cleric with very close ties to Iran, has recently said that with the military withdrawal, American diplomats are now fair game for his militiamen.

Iraq faces a multitude of vexing problems the Americans tried and failed to resolve, from how to divide the country’s oil wealth to sectarian reconciliation to the establishment of an impartial justice system. A long-standing dispute festers in the north over how to share power in Kirkuk between Arabs, Kurds and Turkmen, an ominous harbinger for power struggles that may ensue in a post-America Iraq. A recent deal between Exxon Mobil and the Kurdistan government has been deemed illegal by Baghdad in the absence of procedures for sharing the country’s oil resources.

“We are in a standstill and things are paralyzed,” said Adel Abdul Mahdi, a prominent Shiite politician and former vice president of Iraq, describing the process of political reconciliation between Iraq’s three main factions, Sunnis, Shiites and Kurds. “We are going from bad to worse.”

A surprising number of Iraqis refuse to believe the Americans are really leaving, the effect of a conspiratorial mindset developed over years living under the violent and repressive dictatorship of Saddam Hussein, and a view of history informed by the Crusades, colonialism and other perceived injustices at the hands of the West.

Rani Basil, who drives a taxi in the capital, said, “Iraq will be a great place if the U.S. withdraws,” but he does not believe they will. “I do not think the United States will leave Iraq, because they are about to attack Iran,” he said. In Falluja, where years of block-to-block urban combat left behind a city that its Sunni residents refer to as Iraq’s Hiroshima, residents celebrated the withdrawal with a day of public demonstrations, angry speeches, burning American flags and a gallery exhibition of photos of mangled children, destroyed homes and other signposts of what residents call the bitter legacy of the American invasion.

“It’s a huge happiness that the Americans are getting out,” said Mohammed Adnan, 35. “Hopefully, we are all going to be fine, we Iraqis. We were doing fine before 2003.”

Not everyone was doing fine before 2003. After a failed Shiite uprising at the close of the 1991 Persian Gulf war, Mr. Hussein executed tens of thousands of people, mostly Kurds and Shiites. International sanctions destroyed the economy, creating mass poverty and crime. The dictatorship inflicted deep wounds to the collective psyche, which partially explains why the American invasion unleashed so many unforeseen consequences, from sectarian violence to a winner-take-all political culture.

“If you go to Basra and go house to house, wives will say that their husband disappeared,” said Jana Hybaskova, the European Union’s ambassador to Iraq. “The level of destruction of society was a million times deeper than anyone expected.”

Experts estimate that the remains of 250,000 to 1 million Iraqis lie in mass graves around the country, victims of the Hussein regime. Not a single victim has been identified by DNA analysis, partly because various government ministries and the two factions with the greatest claims of victimhood — the Kurds and Shiites — have been unable to agree on how to proceed. The lack of a painful but cathartic process of reckoning with its history — as South Africa and other countries have done — has stymied Iraqi society’s ability to vanquish the ghosts of its past.

While more than 100,000 Iraqi civilians perished in the war and its aftermath, violence has decreased significantly since 2007, when there were almost 7,500 attacks a month. But Iraq remains an extremely dangerous place. According to the American military, there were 500 to 750 attacks a month this year, including bombings, rocket attacks and assassinations. There are still roughly a dozen insurgent groups and militias active in Iraq: Sunni groups made up of former members of the ruling Baath Party and the home-grown insurgent group, Al Qaeda in Iraq; and Shiite militias supported by Iran and Moktada al-Sadr, the anti-American cleric.

While the violence has declined, sectarian rifts still have not healed. American officials worry that a large attack on a Shiite shrine could trigger a new round of sectarian bloodletting. It remains unclear whether Iraq’s security forces are loyal to their nation or their sect. In Abu Ghraib, the Sunni stronghold outside Baghdad, residents complain about harassment by the Shiite-dominated security forces and say they fear them more than insurgents. Local police and army outposts fly the flag of Imam Hussein, the revered Shiite martyr.

“It’s been papered over,” Joost R. Hiltermann, of the International Crisis Group, said of the sectarian divides. “There has been no reconciliation whatsoever.”

The war opened Iraq’s tremendous petroleum reserves to foreign investment for the first time since 1974, though American companies did poorly in the postwar auctions. So far, the Ministry of Oil has granted 12 licenses for fields in the south to companies from Britain, China, Korea, the Netherlands, Russia and elsewhere, with just one going to an American firm, Exxon Mobil. The outcome helped defuse criticism that the United States had invaded Iraq for its oil.

Yet, Iraq’s oil output still has not rebounded to the level it was in the late 1970s, according to the International Energy Agency. The Iraqi government’s stated goal of raising output to 12 million barrels a day by 2017, from the current 2.95 million barrels a day, is regarded as unattainable by some analysts.

Aesthetically, Baghdad is still a warzone of checkpoints, blast walls and coils of razor wire, where buildings sit partially destroyed from the first wave of bombings that President George W. Bush called “shock and awe.” At entrances to the garrisoned heart of the central government, the Green Zone, vehicles on the way in are searched for bombs, and on the way out for kidnap victims. And most Iraqis still receive only a few hours of electricity a day, forcing many to sleep outdoors on their rooftops during sweltering summer nights. Iraq has improved in some respects. Life in Baghdad has blossomed in recent years — street life has returned, markets are bustling, a new amusement park is opening and even the circus came to town this year. The government of Prime Minister Nuri Kamal al-Maliki, while hamstrung by sectarian infighting, was chosen in elections last year that international monitors declared as free.

On the garbage-strewn banks of the Tigris, a group of young men who lived abroad during the bad days now gather on Fridays to ride their Jet Skis, arriving in the early afternoon and staying until the sun goes down. They drink Turborg beer and Chivas whiskey, and listen to American pop and rap music.

“When I’m on the Jet Ski on the Tigris, I forget all the explosions and the politics, everything,” said Khaldi Nuami, who owns an import-export company whose primary product is armored cars.

A palpable sense of melancholy pervades Iraq. The war opened a generational divide that splits older Iraqis, who recall a brief golden age in the 1960s and 1970s, from younger ones, who are drawn more to the culture and ideas the Americans tried to import here.

“In the 1960s, life was good,” said Qassim Jasim, who has baked bread at the Abu Naseer Bakery in Adhamiya, a Sunni enclave in the capital, for 38 years. During the war, he said, “when I used to go out and see the dead bodies, I would cry for what it used to be like here.”

His neighborhood is no longer a bloody battleground controlled by Al Qaeda. But the Shiites have mostly left. The fabric of the community has been forever altered. Ghaith Raad, whose family owns a well-known sweets shop across the street from the bakery, returned from Syria — where he fled during the fighting — about four months ago.

“When I came back, I didn’t find any of my friends,” he said. “The society has changed here, the people have changed.”

The war opened Iraq to the outside world. For the first time, Iraqis had easy access to satellite television and the Internet. This allowed the pop singer known as Dali, who left Iraq in the 1990s as a child and became famous in the Arab world, to become a star in her own country. She recently returned for the first time since the war began, to perform and film a music video.

Over coffee in the lobby of the recently refurbished Ishtar Sheraton hotel, dressed in a purple Adidas track suit, she considered her homecoming.

“I can feel how Iraq has changed now, and how it is sad,” she said. “All of Iraq is sad.”