How it happens in the first place: just as pimples and acne occur on your face, butt acne is caused by bacteria getting trapped in the area. Mainly it is caused by clothes trapping bacteria in the area, especially when sweat occurs.
How to prevent it for the future: try to avoid too-tight clothes on that region so that bacteria can’t get trapped to the skin. And always be sure to change your clothes as soon as possible after the gym or a workout. Your underwear can also have an effect, so try and stick to breathable materials (like cotton).