(Sarah Parnass,Louisa Loveluck,Mustafa Salim/The Washington Post)

As the sun sank over Iraq’s northern city of Mosul on Tuesday, sounds of elation filled the air.

Families cheered and sang as they clutched their national flag, drivers blasted their horns, and, for a moment, it seemed that the city was united in victory against the Islamic State. 

Across the country, the party had also started. Prime Minister Haider al-Abadi’s declaration Monday that the battle was officially over after nine months sent revelers pouring into the streets of Baghdad. In the southern city of Basra, fireworks crackled late into the night.

The celebrations provide a much-needed respite for a nation that was already unraveling when the Islamic State arrived in Mosul three years ago. Defeat here is a heavy blow for the militants, robbing them of one of their most important strongholds and dashing their dream of a proto-state.

But viewed from the ground, Iraq’s victory is a messy business.

How Iraqi forces defeated the Islamic State

No one knows how many people died during the fighting. Half the city’s residents were displaced; its landmarks and most populous districts are shattered beyond recognition.

The example of other cities recaptured from the Islamic State suggests that the Iraqi government may struggle to rebuild and to resettle the most vulnerable, most of whom are  now packed into displacement camps or living with relatives.

Sitting still and stunned amid the chaos of an aid distribution point this week, Shaimaa, 17, said she had escaped the fighting alone.

Her sister died in a bombing. Her three brothers have been missing since they were hauled off to an Islamic State prison last year. And her parents? “There was an airstrike,” she said, and that was the last time she saw her mother.

“I saw my father’s body in the rubble and I walked away. We got our city back, but there is nothing for me in it.”

Every family around her said they had lost someone to an airstrike or Islamic State shelling. Sometimes that meant one child; other times it meant five.

Looking back, the fight to retake the city now resembles two separate battles. First, Iraqi security forces took the eastern half of the city, declaring it secure by the end of January. Then they moved on the more densely populated west, relying heavily on U.S.-led coalition airstrikes and taking heavy casualties as they went.

The eastern districts have sprung back to life. Fruit sellers line the roads, peddling melons and mulberries in the heat of the day. And shops run a roaring trade, packed at lunchtime and bustling with life as the sun sets.

But cross the Tigris River heading west, and the cityscape shatters into an ugly sea of broken buildings. 

In the Old City, a final redoubt for Islamic State militants this week, it can be hard to tell where one structure ended and another began. 

The only way through some alleyways is over collapsed homes. And as one clambers, one sees remnants of lives they once held. Baby clothes and a cheese grater were mixed into the rubble of one house. The stench of rotting flesh suggested the occupants were still there, too — buried, somewhere, deep under the rubble.

Security remains a huge challenge in a city that had a population of more than 2.5 million before the war. The militants have already proved their ability to launch wildcat bombings in districts long retaken by the security forces. Residents are uneasy about the prospect of sleeper cells, sometimes fearing that a weak rule of law will allow extremist fighters to walk free for the cost of a bribe.

“Of course their men are here; everyone knows it,” said Ahmed Wadallah, tending a Mosul nut shop Tuesday. His family has installed security cameras at the door to record the movements of men they know to have joined the Islamic State.

On the eastern bank of the Tigris River, an old fairground is now a screening station to stop fighters from leaving among the civilians. Inside an old bumper-car rink, dozens of men sat in rows last week and waited for their judgment. Military intelligence officers in balaclavas sporadically moved among them to pull out an evacuee accused of working with the militants. 

Some went quietly. Others wanted a fight.

“I swear I only prayed in their mosque. I have nothing to do with them,” shouted one man, his back covered in what appeared to be fresh welts. 

A thickset officer with his face covered waved to his colleagues to drag the man away. 

“You were walking through the streets with your gun,” he said. “We saw you.”

The man hung his head, then began to cry.

Mustafa Salim contributed to this report.