Trailing 1-0, it had looked like the highly fancied Serbs and group D champions were going to bow out of the tournament late in the second half, but substitute forward Saponjic found a stoppage time equaliser to send the match into extra time.
Hungary, which advanced from group C with just one win (against Korea DPR) from its three matches, went into the match as the firm underdog.
However, it went 1-0 up courtesy of Bence Mervo's goal on the counter-attack.
His fifth goal of the tournament came just moments after Serbian forward Filip Jankovic missed a guilt-edged chance to score at the other end.
Jankovic, filling in for the suspended Sergei Milinkovic, who had to sit the game out after receiving a couple of yellow-cards during the group stage, should have scored after Nemanja Antonov put the ball on plate for him.
But he miss-hit the ball and Hungarian forward Zsolt Kalmar opened Serbia up on the counter with a strong run into the box, before dragging the ball back for Mervo to blast home.
The Serbs, who botched endless chances through the first 90 minutes, finally found the equaliser in stoppage time through substitute forward Ivan Saponjic.
His goal, a powerful header via a corner a kick, was a bitter blow to Hungary, which had done so well to keep the Serbs in check for the majority of the match.
Serbia 2 (Ivan Saponjic, 90+1; Attila Talaber (OG), 118)
Hungary 1 (Bence Mervo, 57)
Extra time: 2-1
Halftime: 0-0