The explosion hurts the hero without hurting engulfing monsters. It also scatters the bag's contents around without putting them inside the engulfing monster like dropped items.
The problem code is the object placement code in UnNetHack's dump_container(), which fails to account for engulfing monsters.