A quickly taken Xavi corner resulted in the ball being fed short along the bye-line to Villa and, from his quick pass, Silva whipped the ball in to Torres who stretched to prod it home at the near post.
Sweden had created nothing up to that point yet they should really have been level seconds later.
Henrik Larsson cleverly shielded the ball on the edge of the Spanish area before playing in Elmander but he shot wastefully into the side netting.
Spain were still having all the possession but they were struggling to make it count and it was Lagerback's more direct approach that began paying dividends.
Ibrahimovic's lobbed pass put Larsson clear but, although the veteran striker had to wait for the ball to come down in the area, he will still be disappointed with his eventual shot, which he spooned high over the bar.
While Sweden were pumping balls into the area they remained dangerous, however, and it was through that method that they equalised.
Spain were undoubtedly missing Carles Puyol, who had limped off injured, when Stoor fired in a cross from the right and Ibrahimovic had time to take the ball down and slot home.
The big striker's first touch was poor but he easily shook off a weak challenge from Sergio Ramos and although Iker Casillas got a hand to his shot, he could not keep it out.
Spain had gone quiet by this point but they still should have had a penalty just before the break.
Zlatan Ibrahimovic finds space to score Sweden's equaliser
Silva was preparing to meet a Ramos cross from the right when he was blatantly pushed over by Elmander but referee Pieter Vink chose to ignore the challenge.
Sweden could be pleased with their work in the first 45 minutes and it appeared they were happy with a point, taking off Ibrahimovic to save his troublesome knee, and sitting deep as the second period began.
Their reluctance to come forward frustrated Spain, who had used the counter-attack to such lethal effect against Russia.
Cesc Fabregas came off the bench to help create some openings but Spain found Isaksson in solid form when they did get within shooting range.
The Manchester City keeper spilled a low Silva shot but recovered to keep out Villa's follow-up before Torres' shot was blocked by Daniel Andersson.
Isaksson made another smart stop to keep out Marcos Senna's stinging effort on the run soon after and it looked as though Sweden had done enough for a draw.
But Villa had other ideas, latching on to Joan Capdevila's pass in the 92nd minute and expertly sliding the ball into the corner of the net.
David Villa's strike was the second winning goal at Euro 2008 that was scored in injury time, following Arda Turan's winner for Turkey against Switzerland.
There have now been seven winning goals in the 90th minute or later (excluding extra time) in the history of the European Championship.
Three of these goals have been scored by a Spanish player. Antonio Maceda, in the 0-1 win over West Germany on 20 June 1984, and Alfrono, in Spain's 4-3 victory over Yugoslavia on 21 June 2000, were the others.
Villa is the top scorer of the Euro 2008 tournament, and now Spain's record goal scorer at European Championships with four goals. He has netted a combined seven goals at European Championships and World Cups, more than any other Spanish player.
Spain extended their unbeaten streak in all competitions to 18 matches (16 wins, two draws). Their last defeat was against Romania (0-1) on 15 November 2006. Romania is a possible opponent for Spain in the quarter-finals.
Spain are on a streak of eight straight wins. Their record of nine dates back to the 1920s. They have also won six competitive matches in a row. Their national record is seven.
This was Luis Aragones' 50th match as Spain's head coach. Spain have won 35, drawn 11 and lost just four of his matches in charge.
Sweden suffered their first loss at the European Championship since 19 June 2000 against Italy (2-1), a streak of five matches.
Carlos Marchena's yellow card was Spain's 50th card in European Championship history, a total made up of 49 yellow and one direct red. Only Germany (55) and the Netherlands (51) have received more cards.
Bookmark with:
What are these?