(CNN) -- David Villa scores a dramatic injury time winner to give Spain a 2-1 victory over Sweden in Innsbruck to put them in the quarterfinals of Euro 2008 by finishing top of Group D.
David Villa leads the race for the Golden Boot after his fourth tournament goal saw Spain defeat Sweden 2-1.
The match had looked set to end in a draw after Zlatan Ibrahimovic had equalized following Fernando Torres' 15th-minute goal.
However, a long ball out of defense dropped kindly for Villa and the striker slipped the ball to the left of goalkeeper Andreas Isaksson for his fourth goal of the tournament.
The result leaves Spain with six points in Group D, three more than Sweden and Russia and certain of top spot after beating both those teams.
Torres had put Spain in front when he lunged at a pass from David Silva, with his right foot outstretched, to touch the ball past Isaksson.
Sweden veteran Henrik Larsson had shot over the bar and Johan Elmander, who scored against Spain in a qualifying match for Euro 2008, missed two chances before Fredrik Stoor curled a pass from the right to Ibrahimovic in the area in the 34th minute.
The Inter Milan striker turned away from marker Sergio Ramos and hit a low shot that just made it under goalkeeper Iker Casillas.
Spain controlled early on and it was no surprise when Torres put them ahead with his first goal of the tournament. Silva chipped a diagonal pass into the area toward Joan Capdevila, but Torres stuck out his right foot to intercept and touch it in for his 16th goal in 51 appearances for Spain.
Larsson and Johan Elmander both had their chances before Stoor found Ibrahimovic at the far post.
Ibrahimovic initially tried to volley Stoor's cross from the right and regained control before shrugging Ramos to the ground. He then swerved to score with a low right-footed shot that Casillas got a hand to but could not keep out.
It was the second straight match in which Ibrahimovic had scored, after he ended a near three-year scoring drought in a 2-0 victory against Greece.
Villa was in the team despite sustaining a hairline fracture of his right index finger while celebrating the first of his hat-trick goals against Russia, and the Sweden defense kept him under wraps until the 38th minute, when Isaksson had to stop a low shot.
Elmander, who started in place of the injured Christian Wilhelmsson, was troubling Spain down the flank but was lucky not to concede a penalty for a nudge on David Silva just before halftime.
Markus Rosenberg came on for Ibrahimovic to start the second half, with Sweden not wanting to risk their star player's swollen left knee.
There were few clear chances in the second half, and those that came went to Spain. Sweden's attack sorely lacked the physical presence of Ibrahimovic, and Larsson just failed to meet Petter Hansson's cross at the far post in the 79th minute for his team's best opportunity.
At the other end, Villa jumped on Silva's rebound in the 63rd minute and fired straight at Isaksson from close range. Villa then recovered the loose ball and centered to find Torres, whose shot was blocked.
The teams seemed content with a draw that would have helped both, but Sweden was caught out when Capdevila sent a 40-meter pass over the head of Torres and into the path of Sweden defender Hansson.
However, Hansson lost the ball under pressure from Villa, who calmly scored with an angled shot from just inside the area.
"We're happy. We had many chances in both halves and we managed to score right at the end," said Spain coach Luis Aragones. "In the second half, we went out looking for the goal and things finally worked out."