Hernan Perez struck deep into injury time as Villarreal scored twice in the final 10 minutes to come from behind and beat Malaga 2-1.
After an early Marcos Senna effort had rattled the crossbar it was the visitors who largely controlled proceedings and they deservedly took the lead when Santi Cazorla came back to haunt his old club with a left-foot shot from the edge of the area that gave Diego Lopez no chance.
However, Villarreal rallied in the final quarter and were handed a route back into the game when Carlos Kameni up-ended Perez in the box with seven minutes remaining.
The goalkeeper was sent off and Senna duly tucked away the penalty.
Perez capped a remarkable comeback four minutes into stoppage time as he slammed the ball home from outside the box to give Villarreal a vital three points in their battle to avoid relegation from the Primera Division.
Malaga had come into the game on the back of a run that had seen them take 19 points from 24 and rise to third in the table...