Substitute Jesus Navas tapped home two minutes from time to snatch a 1-0 victory over Croatia and send holders Spain through to the last eight of Euro 2012 as Group C winners.
Croatia, who battled hard against the world and European champions, went out as Italy finished second thanks to a 2-0 win over Ireland. Spain will now face the runners-up in a Group D featuring France, England, Ukraine and eliminated Sweden.
Spain topped the group on seven points, with Italy on five and Croatia on four, while Ireland finished with zero.
Spain's late winner was tough on Croatia and they probably had the best chance of the game before Spain's goal when Iker Casillas brilliantly blocked Ivan Rakitic's close-range header in the 59th minute.
However, as the game opened up in the latter stages, Spain substitute Fabregas and fellow midfielder Andres Iniesta were able to carve open the Croatian defense and Iniesta squared the ball for Navas to slid home into an empty net.
"It was a very tense match, very tough," said Spain coach Vicente del Bosque. "They closed ranks at the back very well and didn't give us much space.
"We threw on Cesc (Fabregas) because we needed not only possession of the ball but someone to get forward into goalscoring situations.
Goalscorer Jesus Navas added: "It was a tough match and they managed to play their game well. It was difficult to penetrate their defenses. We are the kind of players who go all out for victory because that is what we believe in and what we want."
On a warm and breezy night at Gdansk's amber-colored PGE Arena, Spain quickly seized control of possession in the first competitive meeting between the two sides.
The Croats, with in-form Mario Mandzukic operating as a lone target man up front, looked to catch Spain on the break but had to wait until the 25th minute for their first goal attempt.
Playing in unfamiliar light-blue shirts, Spain struggled to create any clear chances and centre backs Sergio Ramos and Gerard Pique had probably their best two efforts of the first half with long-range strikes.
Spain coach Vicente del Bosque fielded an unchanged side from last week's 4-0 drubbing of Ireland, with Fernando Torres leading the line and Iniesta and David Silva operating on either flank.
Torres had a quiet game and was replaced by Fabregas, who operated as a roving forward in Spain's 1-1 draw with Italy and had netted the equalizer.
Del Bosque's Croatian counterpart Slaven Bilic, who is joining Lokomotiv Moscow after the finals, switched to a more defensive formation, with in-form striker Mario Mandzukic ahead of an offensive midfield trio led by playmaker Luka Modric.
It was Modric who conjured a superb pass with the outside of his foot to create the chance for Rakitic but he was otherwise well shackled by the Spanish midfield and defense.
"I'll have several sleepless nights after missing a great chance to give us the lead," said midfielder Rakitic. "But we have no regrets because we left it all out there on the pitch, we played our hearts out and we can bow out with our heads high.
Coach Bilic added: "I can only congratulate my players because they played a heroic match but unfortunately we didn't win because we didn't take our chances and we always knew they would come few and far between against the Spaniards.
"The game plan was to thwart Spain in the first half and have a go at them in the second. It worked and we did everything right except score.
"We came here to go all the way and we failed because we lacked that tiny bit of luck that makes all the difference. I think my staff and I are leaving behind an excellent generation which has every chance of improving."
(Additional reporting by Zoran Milosavljevic; Editing by Ken Ferris)