Siem De Jong opened the scoring for Ajax after 10 minutes, but goals by Maarten Martens (24) and Charlison Benschop (32) had AZ in command before De Jong's second after 37 minutes sent the sides in at the break on level terms on Thursday.
Rasmus Elm struck the winner from the penalty spot after 55 minutes to send AZ through and into a last-eight meeting with amateur side GVVV.
Today's meeting at the Amsterdam ArenA was a rerun of the match that was abandoned before Christmas when AZ goalkeeper Esteban Alvarado was attacked by a fan.
Alvarado was sent off after retaliating, prompting AZ coach Gertjan Verbeek to withdraw his players from the pitch and the match to be abandoned.
Initially the KNVB ordered Thursday's game to be played behind closed doors, but relented in favour of the match being played in front of a 22,800 crowd consisting mainly of children from local schools and sports clubs.
The two sides go head to head again in the Eredivisie at the AFAS Stadion on Sunday. AZ lead the way in the Dutch top flight by a point from PSV Eindhoven, with Ajax currently fourth, five points off the summit.