Following a five-game winless run, the Morocco international was on hand to help Asier Garitano’s men return to winning ways and end the season on an impressive note with his first league goal for the club.
The Cucumber Growers found themselves behind just 20 minutes into the tie after Joel Campbell found the net from Ruben Castro’s assist.
Their problems were further compounded when Diego Rico was sent off in the 24th minute after picking his second booking in the tie.
However, the Estadio Butarque outfit braced up and levelled matter four minutes after the incident.
In the 64th minute, Jose Naranjo gave the home team the lead for the first time in the game but Antonio Sanabria restored the lead to the visitors 12 minutes later.
Amrabat, who replaced Jose Naranjo in the 74th minute, then notched the all-important goal five minutes after his introduction to help his side wrap up the season on a high.
Leganes escaped relegation by a whisker after finishing just a place above the drop zone with 43 points from 38 matches.