“Thank God I managed to hit Abadi with stones to take revenge for the kids,” Mr. Ahmed said.

As those scenes unfolded and with anger swelling in the streets of the capital, many are now sure to wonder how long Mr. Abadi may remain in power; at the very least, the chaos is likely to presage the resumption of street unrest that had calmed during Ramadan and the military operations in Falluja.

The scenes that unfolded across the city on Sunday were another brutal illustration of the paradox Iraq faces as its security forces — and the American military, which is training the Iraqi Army and carrying out airstrikes and raids by Special Forces — make gains against the Islamic State. As more territory is won back, the group is reverting to its roots as a guerrilla insurgency, turning Baghdad again into an urban killing field.

Assaults like the one early on Sunday, as well as a string of attacks in Baghdad in May that killed more than 200 people in a week, make it difficult, if not impossible, for Mr. Abadi, a Shiite, to make meaningful progress in reconciling Iraq’s majority Shiites with Sunnis.

But the ferocity of the attack, and the ease in which the Islamic State is able to carry out mass murder in Baghdad, demonstrate another monumental challenge if the extremist group is driven from areas under its control: Not only will reconciliation be paramount, but any lasting peace will also require a lengthy counterinsurgency campaign that will challenge the Iraqi security forces and, perhaps, require a deepening involvement by United States forces.

After Mr. Abadi was forced to retreat with his bodyguards, he issued a statement saying that it was his “moral duty” to visit the site of terror attacks, and that he understood “the feelings and emotions and the actions of some people in their moment of sadness and anger.” He also declared three days of national mourning for the bombing victims.