That also explains why it didn't happen all the time since the heater turns off and on via a thermostat. I bet you were getting shocked when it was trying to heat. I'm glad you found the problem because although you said nothing in the tank seemed to be gettign harmed, it might end up eventually being bad enough to hurt things. I've had a couple of pumps seize and rupture and kill all my inverts. Not real fun.
Also (and I've made this mistake myself), "electrocution" is death
by electric shock, not just electric shock.