The Dark wizard Agahnim is a priest, who, by unknown circumstances was possessed by Ganon the king of thieves. Ganon hoped that, by using Agahnim, he could break the seal that was put on him by the seven wise men, whom are synonymous with the seven sages. Many believe this is the very same seal from Ocarina of Time.
"Some day when this seal is broken that is when I will destroy your descendants As long as the Triforce of Power is in my hands"
-Ganondorf Ocarina of Time
In the intro to A Link To the Past, it is revealed that, prior to the events that transpire in the game, Hyrule was suffering from many hardships due to unknown causes. When there seemed to be no solution, Agahnim appeared as if from nowhere displaying great mastery of the arts of magic. Using these powers, Agahnim quilled the plagues, thus returning prosperity to the kingdom. Much in his delight, the king of Hyrule, thinking Agahnim to be a descendant of the Sages, invited him into the royal household. Once established, Aghanim began his vile plan. He eliminated the good king of Hyrule, brainwashed and took control of the royal guard, and set out to gather the descendants of the Seven Wise Men, offering them each as sacrifices in order to break the seal placed on the Dark World.
Agahnim is encountered twice during the course of A Link to The Past, Once in the Oracle Series, and once in Link's Awakening, when the Nightmares take on his form. Each battle is approached in the same way. Because it is impossible to harm Agahnim directly, Link uses his sword to reflect Agahnim's magic blasts in order to inflict damage.
The first time Link encounters Agahnim is during A Link to the Past when, after retrieving the master sword, he is contacted telepathically by princess Zelda's plea for help. After going to the sanctuary and finding the dying sage, Link returns to Hyrule Castle,and ascends the tower, only to find Agahnim completing the sacrificial ritual. Overcome by anger Link battles Agahnim. Once Agahnim realizes he has lost, and that he is close to death, he uses the now open gate to draw Link into the Dark World, making his escape.
The second encounter also takes place in A Link to the Past. After Link gathers the seven maidens from the Dark World. The maidens use their powers to break the protective barrier surrounding Ganon's Tower. Link enters the tower, and after completing the immensely difficult dungeon, finds himself at the door to the final chamber. Believing that the final battle with Ganon is near, Link enters at the ready. Much to his surprise however, Link finds Agahnim awaiting him. The two participate in a battle, similar in nature to the first, except for the fact that Agahnim produces two copies of himself, which add to the difficulty of the battle. Even though Agahnim fought harder, Link had traveled across two worlds, gaining much strength and ability since their last encounter. Because of this, Link was able to finally strike the fatal blow, ending the life of the evil wizard, or so he thought. Agahnim was dead, but the soul that resided within him was merely expelled returning to its own body. The soul was none other than that of Ganon. Link called upon the bird statue, using his Ocarina, and with it followed Ganon to his place of hiding, where one of the most epic final battles in Zelda history was bound to take place. After felling Ganon with the Silver Arrows, Link claimed the Triforce, returning peace to the land at last.
The third encounter takes place in Link's Awakening during the final boss fight of the game. After completing his quest and entering the Wind Fish's egg, Link battles his own nightmares in order to awaken the Wind Fish, and end his own dream. The nightmares take the forms of different enemies, notably Ganon and Agahnim from A Link to the Past. The battle is almost exactly the same as the other two encounters, ending in the nightmares simply changing form.