A late Lars Bender goal in Lviv saw Germany beat Denmark 2-1 and top Euro 2012 Group B, setting up a quarter-final against Greece.
Lukas Podolski put Germany in front in the 19th minute, but a goal from Michael Krohn-Dehli gave the Danes hope with 45 minutes to go.
However, Bender's 80th-minute strike ensured the Germans retain a 100% record at the tournament, with Denmark finishing third and going home.
Germany will play Greece in the second quarter-final in Gdansk next Friday.
Bayer Leverkusen midfielder Bender, playing as full-back for the national team, struck as the Danes were pushing for a goal which would have taken them through to the last eight at their opponents' expense, and just after Morten Olsen's side had seen a strong penalty appeal turned down.
Germany controlled much of the game, but were nevertheless made to sweat as Denmark's belief and Portugal's 2-1 win over Holland ensured a tense finale which ended in their favour, clinching them top spot in Group B with a perfect record.
Germany coach Joachim Low made his first change of the tournament, replacing the suspended Jerome Boateng with Bender, but he resisted the temptation to make further alterations to his winning side.
Niki Zimling was passed fit to play by Denmark coach Olsen, while the injured Dennis Rommedahl's place was taken by Jakob Poulsen.
Low's side began with authority and the diligent Thomas Muller went close twice early on, but Stephan Andersen showed lightning reactions to deny his second attempt in the sixth minute.
Germany remained camped in the Denmark half, playing a patient yet expressive game which led to Podolski marking his 100th Germany appearance with a goal in the 19th minute.
Muller provided a low cross from the right-hand side of the penalty area which Mario Gomez flicked intelligently back to Podolski, who left nothing to chance with a powerful shot from seven yards out.
Germany's lead lasted only five minutes, though, before Krohn-Dehli's second goal of the tournament brought the Danes level.
He diverted Nicklas Bendtner's header past Manuel Neuer from six yards out from a well-worked corner after a positive reaction from the Danes to falling behind.
Towards the end of the first half, Podolski blazed a free-kick over the bar from the edge of the penalty area and Sami Khedira prodded a shot wide from 10 yards out as Denmark were again forced to defend.
Jakob Poulsen went within inches of putting Denmark in front five minutes into the second half when his shot grazed the outside of the left-hand post with Neuer beaten.
Low sent Andre Schurrle on for Podolski midway through the second half and his first touch of the game brought an excellent one-handed save out of Andersen.
Neuer showed less sophistication in dealing with Zimling's distance effort in the 71st minute, conceding a corner as Denmark enjoyed another bright spell.
Five minutes later, Bendtner got in around the back and tried at full stretch to lift the ball over Neuer, but without success as he had his shirt pulled by Holger Badstuber.
One goal for Denmark was still required to send them through and send Germany home since Portugal had taken the lead against Holland, but just as Low's side were showing the first signs of tension, they regained the lead from an unlikely outlet.
Mesut Ozil's throughball was intended for Miroslav Klose, but it was too far in front of the Lazio striker but as William Kvist slipped, it became inviting for full-back Bender, who coolly took his first international goal in the 80th minute to take Germany through and knock Denmark out of Euro 2012.