BAGHDAD — Iraqi troops and their militia and volunteer allies were on the verge of declaring victory over Sunni militants holding the strategic town of Tikrit and were about to hoist the Iraqi flag over key government buildings, when, a survivor recalled Wednesday, “the doors of hell opened.”

The Iraqi forces had apparently walked into a trap, and some soldiers — and many more of their untrained volunteer supporters — were either killed or badly wounded when the Islamic State in Iraq and Syria sprung it, according to accounts from two soldiers and volunteer leaders reached by telephone afterward.

The debacle in Tikrit on Tuesday offered a vivid illustration of how badly the Iraqi military needs advisers. For weeks, the Americans had implored Iraqi leaders not to fight for the centers of cities, but to establish control of roads and highways, and thus set their own conditions for battle. But the 300 American military and intelligence advisers now in the country are not, as of now, working directly with troops and commanders at the front.

The ambush in Tikrit, a few miles from the birthplace of Saddam Hussein, was also a piercing reminder of how difficult it will be to roll back the military gains made by ISIS in June when the Sunni militant group, bolstered by other Sunni insurgents, took control of Mosul after the army melted away, then pushed to Tikrit, the provincial capital of Salahuddin. A look at the map of Iraq suggests that the insurgents have opened far more fronts than the Iraqi security forces can possibly deal with at once.