The Danish lead was five goals at the break after great play by Niklas Landin Jacobsen, who had a save percentage of a whopping 50 after the first 30 minutes of the semi-final.
Denmark held the lead at the start of the second half - but then the Spaniards woke up.
Four straight goals meant 19-20 and Spain had created new nerves in the match.
The Spanish team also had 20-21 before Denmark managed to create new breathing space.
With 5.30 left in the match, the Danes made it 24-20 and were well on their way to securing the final spot.
But when Mikkel Hansen missed a penalty with 1.10 left and the score 25-23, it opened for a dramatic finish.
Spain also had a great opportunity to reduce, but then Landin Jacobsen stepped forward.
The Danish star goalkeeper saved a Spanish penalty and instead of 25-24, Denmark could score 26-24, which was the final score.
In the final, the winner awaits in the semi-final between Sweden and France, which will be played at 21.00 tonight.