Dear Student
A magnetic compass needle experiences force and shows deflection when a bar magnet or a current carrying wire is brought near it because the magnetic fields of the compass needle and the bar magnet or current carrying wire interact with each other.
When magnetic needle is brought near another magnetic property then like poles will repel. Hence, the needle gets deflected.
Regards