In what turned out to be the match of the tournament so far, Portugal came from behind on three occasions but it was only enough for third place in Group F after Iceland scored a 94th-minute winner against Austria in Paris.
That goal leaves Hungary topping the group, with Portugal having to make do with a clash with Croatia in Lens on Saturday night.
Portugal went into the game eager to show their true ability having only secured draws from their first two matches, but it was more of the same during the opening 15 minutes as they failed to turn possession into chances.
They were eventually made to pay by a Hungary side brimming with confidence but it took some strike to beat goalkeeper Rui Patricio as Zoltan Gera found the bottom corner with a half-volley from 25 yards out.
The goal stunned Portugal and they almost conceded a second soon afterwards when Akos Elek received the ball inside the penalty area, but his left-footed strike was well beaten away by Patricio.
Cristiano Ronaldo had cut a frustrated figure in attack but his quick thinking resulted in Portugal finding an equaliser as Nani latched onto a pin-point pass from the Real Madrid star before driving the ball inside the near post from 10 yards.
Hungary were second best just before the break but they needed less than two minutes to regain the lead after half time through Balazs Dzsudzsak who found the top corner of the net with a free kick, although his effort took a deflection off Andre Gomes in the wall.
Portugal needed an immediate response and they got one through Ronaldo, who found the bottom corner after flicking the ball between his legs after being found by Joao Mario but five minutes later, Hungary were back in front.
Dzsudzsak sent a free kick into the wall but after retrieving the ball on the right flank, he cut inside onto his left foot before lashing a left-footed strike into the far corner, again with the help of a deflection.
Both teams were playing at a relentless pace and it took only seven minutes for Portugal to equalise for a third time with Ronaldo getting on the end of a cross from Ricardo Quaresma to head into the far corner.
Back came Hungary who showed no intent of attempting to see out a draw and they should have gone ahead for a fourth time when Dzsudzsak found Adam Szalai with a cross from the left, but the striker's first-time effort struck the inside of the post and rebounded clear.
However, after Austria equalised against Iceland in Paris, Hungary began to sit back and invite pressure from Portugal but their opponents were unable to create a clear-cut opportunity with Ronaldo volleying the best of them into the ground.
Neither side created a significant chance in the closing stages which left it looking like a showdown with England for Portugal, but the dramatic conclusion to the game in Paris left Hungary to go through as group winners and Portugal having to be content with third position.