Pablo Caballero was the hero for Sporting Gijon as his late header denied Girona promotion, while Racing Santander suffered the drop.
Sporting Gijon secured promotion to La Liga in sensational circumstances on Sunday as a stoppage-time goal from Lugo forward Pablo Caballero cruelly denied Girona, while Racing Santander were dramatically relegated on the final day of the season.
Catalan club Girona led Sporting by two points heading into the final game of the season and Francisco Sandaza's 16th Segunda Division goal of the season just before half-time looked to have secured their place in the Spanish top flight for the first time.
With Sporting winning 3-0 at champions Real Betis, Girona needed all three points to seal second spot, but Caballero's downward header two minutes into time added on made it 1-1.
There was still time for more drama, with the home side having what they thought was a winner ruled out for offside and the linesman who raised his flag was rushed down the tunnel after being struck by what appeared to be a bottle.
The game was abandoned, but when the players returned to complete the final 40 seconds, Pablo Machin's side were unable to come up with a winner and there were emotional scenes at the final whistle as the home side missed out on goal difference.
Girona fans and players were reduced to tears as they face the prospect of a play-off against Real Zaragoza, while Sporting celebrated being back in the top tier after a two-year absence courtesy of goals from Miguel Guerrero, Jhony and Isma Lopez.
There was also heartbreak for Racing, who will make an immediate return to Segunda Division B despite a 1-0 victory at Albacete.
Javier Pinillos' side looked set to pull off a great escape as Osasuna were trailing 2-1 at Sabadell until David Garcia scored his second goal of the game in stoppage time to ensure they stayed up by the skin of their teeth.
Racing would have survived due to their superior head-to-head record to Osasuna, but Garcia's late equaliser sent them down in heartbreaking fashion.
Recreativo Huelva, Sabadell and Barcelona B had already been consigned to relegation.
A 2-2 draw at Leganes was enough for Zaragoza to secure sixth spot and a two-legged tie against Girona, who must somehow pick themselves up from the cruel blow they suffered on Sunday.