I think it’s much the same purpose that underwear serves for the testicles (and penis). As a man, I honestly can’t remember any time I walked around without underwear, but I’m pretty sure it would be worse than with. Things would just flail everywhere.
Other answers about bras are great but I thought the male flip side ought to be mentioned.