Appropriately enough for such an ambiguous TV series, there are two different stories about how The Prisoner's theme song came to be. One story says that series producer/writer/director/star Patrick McGoohan had the tune in his head, but since he wasn't a musician, all he could do was whistle it to composer Ron Grainer, who then came up with the theme. This story is widely regarded as a load of dingo's kidneys, though that didn't stop the producers of The Prisoner Video Companion from repeating it.
The other, more widely accepted story, is that Grainer already had a piece of music on hand called "The Age of Elegance". When McGoohan asked him for some theme music, Grainer played him "The Age of Elegance" and McGoohan hated it. Too slow. Grainer responded by reworking the piece, speeding it up, giving it more oomph. He played the new version for McGoohan, and this time he loved it, and that's the theme music that the series wound up with: