 Alright, so why did the needle inside the compass deflect? Well, turns out when we brought the two ends of the wiring contact with the terminals of the cell, current started moving in the wire. And as a result of which, there was a magnetic field which was generated by the wire that was carrying all the current. We have already seen how the needle of a compass can deflect in an area of magnetic field. So how the needle deflected when we brought it closer to a bar magnet. So just like how a bar magnet has some magnetic field. Similarly, a wire carrying a current also has its own magnetic field. And if there is a magnetized needle, such as that of a magnetic compass, it will deflect in that magnetic field. So that's why we see the deflection. Now when we remove the two ends of the wire from the two terminals of the cell, the needle comes back to its original position because now there is no current in the wire. So there is no magnetic field. So the needle of the magnetic compass now aligns itself again with the earth's magnetic field.