Although they started the night seven points clear of rock-bottom Real Zaragoza, Manuel Preciado's men are very much in the thick of the relegation dogfight in the places above.
They appeared destined to stay there too, when Ruud van Nistelrooy cancelled out Alejandro Galvez's opener with just three minutes left.
But, halfway through the time added on, Trejo chested down a pass from Alberto Rivera and unleashed a dipping half-volley that left Willy Caballero grasping at thin air.
Malaga will return home feeling slightly hard done to, especially after enjoying the better of the second half, but beyond Van Nistelrooy's goal they failed to find the back of Juan Pablo's net and paid the price.
Sporting started the better of the two sides and Nacho Novo missed a wonderful eighth-minute chance, somehow firing wide after Gregory's volley was palmed out by Caballero.
Santi Cazorla had Malaga's best chance after that, hitting wide after being allowed to run, before the home side took the lead 10 minutes before the break.
Galvez appeared to be slightly offside as he ran on to Nacho Cases' free-kick, but there was no flag and his outstretched leg did the rest from close range.
Malaga returned in the second half a different team, with Van Nistelrooy signalling their intent with a 12-yard drive that was blocked by Roberto Canella.
The pressure continued and Eliseu crunched a drive against the post from the edge of the box, before Van Nistelrooy got to the ball ahead of Pablo and bundled an effort goalwards, only to see Gregory chase back and hook the ball off the line.
Gijon substitute Mate Bilic then had a close-range hit tipped wide by Caballero, and it appeared to be a crucial save when Van Nistelrooy found the bottom corner as time ticked away, showing pinpoint precision after his first attempt was blocked.
But Malaga were to leave disappointed, as Trejo stole centre stage with the most spectacular of winning goals.