What happens to the needle of a compass kept nearby when electric current is switched on in a wire? Why does this happen?

The current carrying wire behaves like a magnet and it will cause deflection of the needle of the compass. This is called magnetic effect of electric current.