BAGHDAD — Iraqi soldiers and militants of the Islamic State group clashed again over the weekend in a western Iraqi town that has changed hands several times in skirmishes near a military base where American troops are training Iraqi soldiers.

Hundreds of Islamic State fighters captured most of the town, Baghdadi, on Thursday, but by Friday evening Iraqi soldiers had retaken several government buildings.

Then early Saturday, in what has become a familiar routine, the soldiers suddenly withdrew, all but handing the town back to the militants, according to local security officials.

“I have no explanation,” Col. Shaaban al-Obeidi, a commander in a police combat unit in Baghdadi, said on Saturday afternoon, adding that the militants were surrounding a residential complex where hundreds of civilians were staying. “They have put all those families in danger,” Colonel Obeidi said.