Rayo, who had lost nine of their previous 10 games coming into this match, looked certain to be making an immediate return to the Segunda Division as, with Real Zaragoza winning at Getafe just a few miles down the road, they found themselves in the bottom three heading into the final minutes against Granada.
However, veteran striker Tamudo, having started the match on the bench, popped up in the first minute of injury time to head home from close range and give Rayo a stunning victory that keeps them in the top flight.
Granada were also at risk of relegation, but they escape the drop with Villarreal the surprise casualties.
Rayo's miserable form over the last few months meant that even though they started the final day above third-bottom Zaragoza due to a better head-to-head record, the Madrid-based outfit were probably favourites to go down.
And the home side might have found themselves trailing after just three minutes today but for goalkeeper David Cobeno, who denied Allan Nyom with his feet at the expense of a corner.
Ikechukwu Uche and Jorge Carlos Martins also went close for the visitors in the opening 20 minutes before Miguel Michu spurned a great chance for the hosts when he fired wide with only Julio Cesar to beat.
Neither side could make the breakthrough before half-time though, and the nerves would have continued to build in both camps as the match remained goalless heading into the closing stages - especially with Zaragoza winning at Getafe.
Rayo coach Jose Ramon Sandoval had already made two of his substitutions when he made his final throw of the dice in the 76th minute by sending on Tamudo for Alejandro Arribas.
And the move paid rich dividends as the 34-year-old reacted quickest when Medina Piti saw his shot blocked to head in the rebound and save Rayo from the drop.