Agahnim can only be defeated if Link has the Master Sword. He is encountered twice; first in the Hyrule Castle Tower and for the second and last time in Ganon's Tower. He attacks Link with balls of energy and occasionally, bolts of lightning. He warps around the room every time after he has attacked.
In order to defeat Agahnim, Link must deflect his energy balls back at him with the Master Sword. Strangely, this can also be done with the Bug-Catching Net. This is easiest to do when Link positions himself such that his sword swing is directly in front of Agahnim in any of the four cardinal directions. Sometimes, Agahnim will throw a ring of six smaller energy balls, which will fly in all directions after hitting Link, his sword, or a wall, and can not be deflected back at him. Agahnim only performs his lightning attack when in the north center part of the room, facing straight down rather than turning to face Link, so it is advisable for Link to move to either the west or east wall when Agahnim assumes this position.
The second battle is similar to the first. However, Agahnim creates two slightly transparent "copies" of himself, while his true self remains opaque. Though the duplicates can not be damaged, their attacks are real, and can even be deflected into the true Agahnim. Strangely, in this final battle, Agahnim does not use his lightning bolt attack.