The story
From the informal practice of putting ballots into a hat, and then drawing a single ballot from the hat at random to decide the winner. When one put one's own name on a ballot in the hat, one would therefore be eligible to win.
Etymology from Wiktionary, CC BY-SA 4.0.