They will now play the USA on Thursday for the gold medal, but they have already made history by guaranteeing their first-ever Olympic Games podium finish.
Spain made a fantastic start, scoring twice in the opening two minutes of the match.
Hungary pulled one back through a man-up goal as the Spaniards missed opportunities to extend their lead as long-range shots missed the target.
Spain, in blue, did convert chances into goals in the beginning of the second quarter, forging a three-goal lead.
The Hungarians, wearing white caps, managed to find some scoring form to haul their way back into the match, and the teams went into the half-time break with Spain leading 7-5.
The sides traded goals in an evenly-matched third period, which saw both teams score two.
A tense fourth period was edged by Hungary, but Spain hung on to win by one goal.
Anni Espar netted four for Spain, as Roser Tarrago and Maria del Pilar Pena scored twice.
The Hungarians will play Australia for the bronze medal on Thursday.