Dear Student ,
A magnetic compass works on this principle because the earth is considered as a huge bar magnet with its North and South poles aligned along the geographical South and North Poles respectively. Hence, the North pole of the magnetized needle in a magnetic compass is attracted towards earth’s geographic North Pole and the South pole of the magnetized needle is attracted towards earth’s geographic South Pole. Hence, the magnetized needle of a magnetic compass always aligns itself along the North-South direction.
Regards