Tens of thousands of people have fled Samarra, about 60 miles north of Baghdad, in recent weeks, expecting a showdown between U.S. troops and heavily armed groups within the city, according to U.S. and Iraqi sources.

Residents of the city said guerrillas told people to leave neighborhoods in anticipation of a larger battle after a clash on Tuesday in which U.S. warplanes bombed two houses, killing at least four people, according to military authorities.

"I will not go back to Samarra," said Mohammed Mohammed, 37. He brought his extended family of more than 70 brothers, cousins and children to Baghdad this week because of the dangers. "We expect the resistance will be very strong when the Americans go in. And the Americans have no mercy."

Samarra is now controlled by a volatile mix of tribes and gangs, some split along religious lines, and supporters of ousted president Saddam Hussein, according to interviews with numerous Samarra residents who have fled to Baghdad. On July 8, some of those groups launched an attack in which a car bombing was followed by a fierce volley of mortar fire. Five U.S. soldiers and an Iraqi National Guardsman were killed and 40 people were injured.

Even before that, U.S. military authorities had been planning how to retake control of the city without a bloodbath. Officers said they were determined not to let Samarra follow what they call the Fallujah model. U.S. forces made an agreement to stay outside Fallujah, a city west of Baghdad, in order to end fierce clashes there during April. The city is now under the control of insurgents.

"We're not going to make that pact," said Maj. Gen. John R.S. Batiste, commander of the Army's 1st Infantry Division, which operates in the area. "Right now, it is a town where nobody is in charge. That, we will fix. At the end of the day, there will be a city under competent civil government."

U.S. military planners complained in private that Fallujah was a bad deal, allowing the city to become a rallying point and stronghold for guerrilla forces.

"It's the lily pad theory. Fallujah exports itself to Samarra, which exports itself to the next place," said Lt. Col. James Stackmo, an intelligence officer for the division, headquartered in Tikrit. "In Samarra, there's probably 100 to 300 fighters who are holding the town hostage. We're not going to allow a militia in Samarra. We're not going to do it."

The U.S. military will try to mount a joint operation with Iraqi security forces, officials said. Under the plan, U.S. forces would likely seize Samarra in a powerful assault, then have Iraqi National Guard or police officers patrol the city.

"It's not a situation that will necessarily evolve into what we saw in Fallujah in April," said Brig. Gen. Erv Lessel, the Army's deputy operations chief in Iraq. "We learned then how important it is to make sure we have capable Iraqi security forces in place to maintain security when the multinational forces withdraw from an area."

"We've pulled back a little. But don't think for a minute that's permanent," Stackmo added.

But formation of a competent and well-trained Iraqi force has taken more time than officials had hoped. Officials said it has been particularly difficult to mount a contingent willing to take on the challenges in Samarra. The mortar attack on July 8 was aimed at a compound used by U.S. and Iraqi National Guard troops. Since then, many of the guardsmen have quit rather than participate in the fighting in Samarra, according to Mohammed.

"There's no National Guard. And the police in town do their patrols, but they take their orders from the mujaheddin," said Mohammed, who said he had been in Samarra on Tuesday.

"Right now, the mujaheddin and insurgents control everything," said Hassan, another Samarra resident who left the city with his family to move in with relatives in Baghdad. He refused to give his last name, fearing retribution.

Most of Samarra's 300,000 or so residents are Sunni Muslim, but the city also is home to two important Shiite shrines and has a small minority of Kurds. Residents said relations with U.S. forces began to deteriorate last winter after American troops fired on a wedding party that was celebrating in a traditional way by shooting weapons into the air. The wedding party, members of one of the larger extended clans in the city, vowed revenge.

They have been joined by religious militants intent on waging a holy war, by former members of Iraq's disbanded Republican Guard who still sometimes wear their uniforms in the city, and an assortment of other tribes and groups willing to join the fight for sentiment or for money. Some of the fighters came from Fallujah, Mohammed and other residents said, and some came from outside the country.

"Samarra used to drive Saddam crazy," Batiste said. "There are seven tribes. There are two or three cells," he said, and one or two of them are composed of Hussein loyalists. "One is a terrorist cell. There's a criminal family that's always been in Samarra. And there is a lack of competent leadership, partly because there's so many tribes vying."

The groups are threatening and killing anyone who they believe cooperates with U.S. authorities or even with the Iraqi government, the residents said.

"They just go into a house and kill the owner and burn it," Hassan said. Some of the groups also have been enforcing a strict religious code, closing liquor stores and cafes, insisting that women wear head coverings and berating young men who wear bluejeans.

"I was walking down the street when one of them demanded of me, 'Why are you wearing bluejeans?' " said a 17-year-old, who did not want to give his name. "They put a gun to my head. They said, 'Why is your hair so long?' Even before I went back to the house to change, I went to get a haircut."

For several weeks, Samarra residents have been slipping out of town. Many have houses or extended families in Baghdad, and have moved in. Some travel back and forth, depending on their reading of the dangers on a particular day.

A military source in Baghdad cited reports that said as many as 40 percent of the residents have left Samarra. Some residents of the city said the figure was higher.

"The businesses are closed. No one is in the streets," Hassan said. "Any stranger who comes in is looted and robbed. If you go in now, it seems like a city of ghosts."