I think that it was being charged, which causes hydrogen bubbles. But something failed inside the battery, like a cell connector. Since it was being charged, there was a spark and it detonated the hydrogen/air mix above the electrolyte. In any event you should sanitize the area with baking soda and water. You may find some of the paint comes off. And, agree with the others, good thing you are replacing both batteries.
It's possible, of course, that the spark came from the outside of the battery too. Like a short got across it or to ground. Or a connection right by the battery went bad.
I blew a battery up testing a starter once. Made a BANG, lifted the top, split the sides, and let the electrolyte out.