This comes from the British game of cricket, and refers to the bowler who takes three wickets in three balls. If you did this in the 1800's, you were sometimes given a hat by your team.

Or, it could be that the guy gets to wave his cap around.

In either case, the term was first recorded in ink during the 1870s, and its usage has grown, referring to pretty much three of anything w/r/t sports.