The 33-year-old broke El Aynaoui's serve only once but took the match 5-7, 6-4, 7-6, 7-6.
The Moroccan beat Agassi in the first set with fierce serves, but in the second set, Agassi began playing with more aggression and broke his competitor's serve in the 10th game, the British Broadcasting Corp. reported.
Agassi dominated the net to take the marathon match. The ninth game of the fourth set saw the players duel for six deuces.
In the 12th game, El Aynaoui had the chance to square the match, but Agassi refused to yield and fought for the match.
The tennis veteran will face Mark Philippoussis Monday for a spot in the quarter finals.