Cavitation occurs when liquid moving at high speed causes tiny air bubbles in the fluid to form.

The bubbles then collapse, producing a shock wave and the snapping sound.
Pistol Shrimp

Image: Michel Versluis