Why is a hattrick called a hattrick?

You score 3 goals in football it's called a hattrick. Is it because of a magicians "hat" trick or is there some other reason?