The compass needle moved when the wire was connected to the battery because the current-carrying wire created a magnetic field. When an electric current flows through a conductor, such as a wire, it generates a magnetic field around it. This magnetic field can interact with the magnetic field of the compass needle, causing it to move. The compass needle itself is a small magnet, and its orientation changes in response to the magnetic field produced by the current flowing through the wire.
The statement that "the compass needle is attracted to electricity" is misleading; what happens is that the magnetic field associated with the electric current influences the orientation of the needle.