In the accounting of what was won and lost in America’s Iraq war, this sleepy farming town deep in the western desert will rank as a place where almost everything was lost.

It was here, on Nov. 19, 2005, that a group of Marines went on a shooting spree in which 24 Iraqi civilians were killed. Their patrol had been hit by a roadside bomb and one of their comrades was dead. They ordered five men out of a taxi and gunned them down. Then they went into three nearby homes and shot 19 people, including 11 women and children.

On those facts, U.S. and Iraqi accounts agree. On just about everything else — why it happened, whether it was justified and how it was resolved — they do not.

And in those dueling perceptions, over the killings in Haditha and others nationwide, lay the undoing of the U.S. military’s hopes of maintaining a long-term presence here. When it came to deciding the future of American troops in Iraq, the irreconcilable difference that stood in the way of an agreement was a demand by Iraqi politicians for an end to the grant of immunity that has protected on-duty U.S. soldiers from Iraqi courts.

“The image of the American soldier is as a killer, not a defender. And how can you give a killer immunity?” said Sami al-Askari, a lawmaker who is also a close aide to Prime Minister Nouri al-Maliki.

So the troops are going home this month, leaving a question mark over what had been one of the chief goals of the war — to nurture a strategic ally in the heart of the Middle East.

They leave behind a legacy that will forever be tainted in the minds of many Iraqis by the casualties inflicted by the American military on civilians. It’s the raw nerve that jangles, a sensitivity that grates on both sides even as the troops stream out of the country.

The Iraqi government’s decision “has saved the lives of many Iraqis,” said Yusuf al-Anizi, 38, the embittered brother of one of the Haditha victims. “Otherwise, we would have more tragedies to pile on the many tragedies we have seen.”

Exactly how many Iraqis were killed by Americans may never be known. An analysis last year by King’s College London of 92,614 civilian deaths reported from 2003 through March 2008 by Iraq Body Count — a Web site that monitors civilian casualties — found that 12 percent were caused by coalition forces. Though there is no reliable figure for total civilian casualties throughout the nearly nine-year-long war, most estimates put the overall number of deaths at more than 100,000. According to the Defense Department, 4,474 American service members have died, 3,518 of whom were killed in action.

The vast majority of civilian deaths were the result of Iraqis killing Iraqis, whether in bombings or the sectarian bloodletting that engulfed the country in 2005-07, said U.S. military spokesman Maj. Gen. Jeffrey Buchanan.

In most of the incidents of acknowledged violations, such as the prisoner abuse at Abu Ghraib, servicemen and women have been brought to trial, and many are serving prison sentences, Buchanan said.

And in the case of Haditha, there was a thorough investigation, he pointed out. Charges were brought against seven Marines, though they were dropped against six of them and the seventh was acquitted. An eighth Marine will stand trial in January.

“We take all of these things seriously,” Buchanan said. “We do in fact hold trials, and we treat them in accordance with the law. And if they are not found guilty, we’re not going to put people in prison.”

In explaining the breakdown in talks over the immunity issue, the U.S. military blames above all the behavior of private contractors. Buchanan singled out the Nissoor Square incident in 2007, in which Blackwater security guards killed 17 civilians at a busy traffic circle in Baghdad.

‘It’s in the air’

While Haditha and Nissoor Square became potent symbols for many Iraqis, just as vexing were the smaller, often untold incidents of civilians shot dead at checkpoints or near convoys by nervous soldiers fearful that they were about to be attacked, said Peter Van Buren, a State Department official who worked with Baghdad’s Provincial Reconstruction Team in 2009-10. When he arrived, he said, he was struck by the disconnect between Iraqi and U.S. perceptions of the war.

“We tried to convince them we were the good guys and that we’d got rid of Saddam, but given all the killings that had happened, that never hung together,” he said, recalling an occasion when he distributed fruit trees to farmers in a rural area. One refused to accept the seedling and spat on the ground. His son had been killed accidentally by U.S. forces, the farmer said, “and you’re giving me a fruit tree?”

“It’s in the air, it’s in the water, it’s the background music to what we do,” Van Buren said. “The Iraqis remember it even if we don’t. It will be a very dark legacy, and it’s one that will follow us around the Middle East.”

Officers who served acknowledge that such killings soured relations but say there’s little that can be done to avoid civilian casualties in urban warfare. In instances such as the Haditha killings, Iraqis “have every right to be bitter,” said retired Col. Peter Mansoor, who commanded a combat brigade in Baghdad in 2003-04 and then returned as executive officer to the top U.S. commander, Gen. David H. Petraeus, during the surge of U.S. troops in 2007-08.

“In most cases, the circumstances are a lot cloudier,” he said. “The enemy does not wear uniforms. U.S. forces are taking fire, and they hit civilians. It’s harder to assign blame.”

On two occasions in 2003, soldiers under his command killed civilians by mistake — once when a family of six drove unwittingly into the middle of a firefight with insurgents, and later at a checkpoint when a family rushing a child to the hospital failed to stop.

Troops learned lessons as the war went on, he said. They learned to construct checkpoints in ways that made boundaries clearer. After the surge, when soldiers went to live in Iraqi neighborhoods, they learned to better distinguish friend from foe.

“I’m sure those families will never forgive the killings,” he said of the six civilians shot dead by his soldiers. “But when you look at it from the soldiers’ point of view, it was justified. It’s very hard, and obviously it led to a lot of ill will.”

No welcome for Americans

There is no limit to the ill will that envelops Haditha, a pretty, palm-fringed, town of 43,000 bordering a lake in the heart of the desert province of Anbar. Outward signs of the violence that raged have been erased. The bridge over the Euphrates River, on which al-Qaeda in Iraq once publicly beheaded suspected collaborators before it was bombed by U.S. warplanes, has been repaired. The spot where the roadside bomb exploded has been paved over.

The house where seven members of the Hamid family died is empty, and the one where eight members of the Yunis family were killed is occupied by distant relatives.

Only the Anizi family still lives in the squat, dun-colored home in which four male relatives were gunned down in a back bedroom by two Marines. A third kept watch in a nearby room over the brothers’ elderly father, their wives and Khaled, then age 14, the son of one of the men.

Khaled tried to read the names on the Marines’ uniforms when they entered the house, “but they were covered with blood,” he said. “Their hands and vests were soaked in blood. They only wanted revenge. When they came, I could see tears in their eyes. When they left, they were laughing.”

“They are barbarians,” added Yusuf, Khaled’s uncle, the only surviving brother of the victims, who was away at the time.

After the killings were exposed by Time magazine in 2006, the attitude of the U.S. military changed, Yusuf said. The FBI came to investigate. The family received condolence payments of $2,000 for each of the four men. They were promised that those responsible would be brought to justice.

But then the attention faded. Yusuf heard through news reports that most of the charges brought against the Marines had been dropped. The U.S. military left its base in Haditha nearly two years ago, and local officials can’t remember the last time Americans visited the town. They wouldn’t be welcome if they did.

“We wish they never had come,” Yusuf, for whom the withdrawal brings no consolation, no sense of closure said. “The injustice is a bigger crime than the crime itself,” he said. “And now we know for sure justice will never be done.”

Correspondents Uthman al-Mokhtar and Asaad Majeed contributed to this report.