As fruit ages, it starts to ferment, and bacteria break down all the sugars they can, converting them to alcohol. This process introduces carbon dioxide inside the melon.
Similar to shaking a soda can, this can lead to explosive ruptures from the pressurized buildup. The issue can also resolve itself when a crack forms and slowly releases the gases.
While it's not entirely preventable, you can mitigate the problem by storing the watermelon in the fridge to slow down the process. If it's foaming, get rid of it immediately.