Elisabeth Pinedo Saenz and Jessica Alonso Bernardo scored two late goals in the second of the 10-minute extra period, and two goalkeepers combined to stop six of 11 South Korean penalty shots to give Spain the bronze.
South Korea entered the London Games having won a women's handball medal in six of the past seven Olympics.
The team ended 60 minutes of regulation tied at 24-24. In the first extra period, Spain's Macarena Aguilar Diaz scored the go-ahead goal with one minute left for a 28-27 lead, but Jung Ji-hae answered with her first score of the game.
Spain held the final possession but Marta Mangue Gonzalez sailed her shot above the crossbar in the dying moments.
Then in the second overtime, Nely Alberto Francisca and Jung traded goals early, but South Korea went scoreless in the final six minutes.
Backup goalkeeper Mihaela Ciobanu came into the game for penalty situations and turned away four of the five she faced, including three during double overtime.
Alberto Francisca and Negona Fernandez Molinos had five goals apiece to lead Spain. Gwon Han-na had seven goals for the losing side.
South Korea trailed for most of the second half, and Choi Im-jeong tied the score for South Korea at 24-24 with 50 seconds left in regulation. Goalkeeper Ju Hui denied Begona Fernandez Molinos on Spain's ensuing possession, but Jo Hyo-bi's goal on the other end couldn't beat the buzzer.
South Korea lost to Norway in the semis earlier this week. It had beaten Spain 31-27 in the group play on July 28.
South Korean head coach Kang Jae-won said his players' lack of experience showed itself in the close defeat.
"I see this result less as a defeat than an opportunity for our players to gain experience," Kang said, noting that the Korean team was going through a generational change. "I think the experience today was just as valuable as winning a gold medal. I hope that the players can build on this and post better results at the 2016 Olympics in Rio de Janeiro."
The coach said he reserved nothing but praise for his players in the lockers after the match.
"We've made it this far only because every player gave everything she had until the end," Kang said. "They didn't do anything wrong today. It's entirely my responsibility as the head coach that we couldn't come away with a medal." (Yonhap News)