Spain midfielder Santi Cazorla gave Malaga the lead after 65 minutes, firing in from the edge of the area against his former club, but the game was turned on its head in the 81st minute.
Malaga keeper Carlos Kameni brought down Perez for a penalty and was sent off, and Marcos Senna converted from the spot.
Paraguayan midfielder Perez volleyed the winner from outside the area in the 94th-minute to complete the turn around.
Malaga, who are owned by a member of the Qatari royal family, stayed fourth with 50 points and missed out on the chance to reclaim third place from Valencia on 52. Valencia hammered Rayo Vallecano 4-1 on Wednesday.
Leaders Real Madrid edged closer to their first league title in four years with a 4-1 derby victory at Atletico Madrid on Wednesday which put them on to 82 points, four ahead of Barcelona in second.
Villarreal, who started the season playing in the Champions League, moved seven points clear of the relegation zone on to 35 points in 17th place.