Why does the needle get deflected by the magnet?

When the N-pole of compass needle and N-pole of magnet come close to each
other the needle gets deflected due to repulsion, (or) When the S-pole of compass needle and S-pole of magnet come close to each other (S-pole and S-pole are like poles they repel each other) and the needle gets deflected.