Published: August 21, 2006

Rooftop snipers and mortar fire killed 20 people and wounded 300 others as they walked through religiously mixed neighborhoods in Baghdad on Sunday to commemorate the death of one of Shiite Islam's holiest figures, an Iraqi Health Ministry spokesman said.

American and Iraqi officials had been planning security for the pilgrimage for months, trying to avoid the huge loss of life during the pilgrimage last year, when more than 950 died after rumors of a suicide bomber caused a stampede on a bridge packed with pilgrims.

To control crowds better this year, the government banned vehicle traffic in the capital from Friday evening until 6 a.m. Monday, requiring the crowds of pilgrims to walk from their neighborhoods and beyond to the mosque where the body of Imam Musa al-Kadhim, the seventh imam of Shiite Islam, is entombed. The mosque and neighborhood surrounding it, known as Kadhimiya, are named for the imam. (The word kadhim means self-restraint from anger.)

A spokesman for the Defense Ministry said Sunday that the security plan consisted of four ''rings'' to defend the mosque and pilgrims streaming toward it. The first perimeter, around the mosque itself, was made up of armed men from the Interior Ministry, which is tightly controlled by Shiites.

A second ring consisted of Defense Ministry and national police officers at checkpoints around the city, the spokesman said. Iraqi Army soldiers cruising city streets in Humvees made up the third layer of security, with American military helicopters and surveillance as the fourth.

But by early Sunday morning, rival groups were exchanging gunfire on Baghdad's streets, officials and residents said, as processions of pilgrims, segregated by sex, ran into apartment blocs and under highway overpasses for cover.

Sattar Abu Ali, a 42-year-old Shiite who lives in the Slaikh neighborhood in northeast Baghdad, a mostly Sunni area, said gunmen attacked two mosques in the his district at 7 a.m., and later fired into nearby homes.

''I have seen, from inside the yard of my house, gunmen walking and firing randomly,'' Mr. Ali said. ''Also, mortar rounds fell on some houses. In response to that, the people of Slaikh went up to their roofs and started to fire back at the gunmen.''

The American military released a statement late Sunday that seemed to play down the deaths. ''Iraqi military and civil leaders provided a comprehensive security plan to ensure there would be no recurrence of violence that marred last year's event,'' the American statement said. ''As a result, there were no major attacks.''

Haider Ahmed Muhammad, a 38-year-old government worker and a Shiite from the Mansour neighborhood, said he began a six-hour walk to the Imam Kadhim shrine at 8 a.m., avoiding the mostly Sunni neighborhood where part of last year's stampede took place. Beside squads of Iraqi Army soldiers and police officers armed with AK-47 assault rifles, groups of Shiite volunteers handed out juice, water, cakes and chai tea, Mr. Muhammad said.

''The whole situation was comfortable and calm, compared to last year,'' he said.

But he said Sunday proved more violent for a group of his friends, who came under mortar attack as they walked through the Bab al-Muadam district in eastern Baghdad, toward the bridge across the Tigris that leads to the holy shrine. As the group approached the Sunni-controlled Slaikh neighborhood, Mr. Muhammad said his friends later told him, armed men fired their weapons in the air as a warning for the Shiite pilgrims to alter their route.

Undeterred, Mr. Muhammad said, the Shiites moved on to the Sunni area. Snipers then began shooting at them, he said.

Abdul-Wahab Hussein, 47, a high-school teacher, said another gunfight broke out in the same district when armed Shiite militiamen escorting pilgrims confronted a group of shopkeepers to demand they close, as a sign of respect. When the shopkeepers refused, the militiamen fired on them, Mr. Hussein said, wounding two people.

The Defense Ministry spokesman said three men were captured after a protracted exchange of gunfire with gunmen who had fired on the procession of pilgrims.

By Sunday evening, most of the attackers had been killed, captured or driven away, officials said.

Photo: The mother and brothers of 13-year-old Wisam Ali mourned his death yesterday in Baghdad. Wisam was killed on his way to a Shiite shrine. (Photo by Karim Kadim/Associated Press)