Dear Student,
The compass needle itself is a small magnet which is used to detect the presence of a magnetic field. So when we brought it near a bar magnet the magnetic field lines interact with each other.
Therefore the needle deflects to indicate the presence of a magnetic field.
Regards..