A penalty on the final buzzer converted by Valero Rivera decided the unexpectedly close duel between Spain and Hungary in Wroclaw's Main Round Group 2 on Tuesday night.
Spain had a tough nut to crack before their 31:29 (15:15) win, their fourth in their fifth match at this EHF EURO, was sealed.
With the six points they now have on their account, they are level with Germany and Denmark, but the Danes have played one game less.
"Hungary played fantastic, but we fought until the end in one of the best matches during the whole tournament. It took until the last second before this win was secured," said Spain's Alex Dujshebaev.
For Hungary even the nine goals of Laszlo Nagy were not enough to earn their first points at this main round, but definitely it was the best performance they produced so far at this EHF EURO.
"We can be proud of our game after what we showed in the last two matches. We still work hard in the training and hence we started very well," Nagy said.
"After the break the Spaniards took control, but we had at least a chance for a tie. This team is the future."
Spain will face Russia on Wednesday to fight for a semi-final spot in Krakow, while Hungary's campaign at this EHF EURO will come to an end with their game against Sweden.
With speed, power and two strong Nagys on court - Laszlo and Kornel who both scored four goals each in the first 30 minutes - the team of head coach Talant Dujshebaev put Spain under enormous pressure.
Furthermore, the performances of goalkeepers Roland Mikler (Hungary) and Gonzalo Perez de Vargas (Spain) were worth the entrance fee.
Spanish coach Manuel Cadenas had decided for Perez de Vargas to start in goal despite Arpad Sterbik saving an incredible 21 shots against Denmark on Sunday.
After the break, Sterbik had a brief stint on court but was replaced again by Perez de Vargas.
However, despite Perez de Vargas' saves in the first half, Spain led only once - at 1:0 - until half-time.
Hungary played smart, perfectly adapted to the Spanish attack and showed a fluid game themselves.
Spain struggled hard to find their rhythm before the break. Antonio Garcia had the big chance to grant his team a half-time lead, but when he was running towards the empty goal with seconds left on the clock, he shot too early and missed.
With Sterbik on fire at the start of the second half, Spain had the better start, as they forged ahead to a three-goal lead for the first time at 19:16.
But after only eleven minutes of play in the second period Sterbik left the goal again, after Hungary had netted three straight goals and levelled the game once more.
The match was on the edge now, both sides tried hard to decide it in their favour.
While Iman Jamali played a great game for Hungary and Laszlo Nagy scored goal after goal, the successful counter attacks of Victor Tomas and the shots of EHF EURO 2014 top scorer Joan Canellas eventually granted Spain another three-goal advantage at 27:24 exactly ten minutes before the end.
The way to Spain’s fourth victory in their fifth EHF EURO match in Poland was paved, but they needed to wait until the final buzzer when Rivera, with his fifth goal of the match, finally decided the match in their favour.