Basically you're acting as a ground when touching electric parts of the guitar/bass. Use a grounded electrical outlet is the solution.
Actually, the electrical parts of the bass (and the strings, if there is a string ground) are grounding
you, not the other way around. The human body is an extremely good antenna, so it picks up a lot of RF noise which needs to be sent to ground, rather than through the pickups (which is what causes electrical buzz).
On a poorly-shielded bass, the pickups can actually receive RF interference from your own body (not to mention from computers, lights, etc). When you touch the strings or electronics that interference travels through the ground circuit of the bass and out the ground circuit of the amp, attenuating buzz or hum.
Your body isn't actually grounded most of the time either, especially when you have on rubber-soled shoes, are standing on the second floor, a wooden floor, etc.
You can hear how much RF interference your body picks up by touching the tip of a 1/4" cable that is plugged into an amp. You'll notice that you get a loud, godawful sound when you do.
When the amp is grounded trough the outlet it doesn't happen as much.
Exactly. If your body was grounding the bass, it wouldn't matter if you had a grounded outlet anyway.