Achraf Bencharki, who is under fire for a series of uninspiring displays and for failing to agree a new deal with Zamalek, gave his side an early lead and rediscovered some of his form with a fine all-round display.
Central defender Ali Gabr drew Pyramids level shortly after and South African striker Fagrie Lakay put them in front before Zamalek striker Omar El-Said, who set up Bencharki for the early opener, levelled the score at 2-2 on the stroke of halftime following a brilliant assist from Emam Ashour.
Ashour turned scorer early in the second half after being teed up by Bencharki and Zamalek survived late pressure from Pyramids to grab a precious victory over one of their main challengers for the title.
The Cairo giants also gave Ferreira a much-needed win after the canny Portuguese coach, who led the club to a rare domestic double in 2015 during his previous stint at the helm, oversaw a loss and two draws in a disappointing African Champions League campaign.
Zamalek lie second in the Premier League table with 28 points, two ahead of third-placed Pyramids.
Leaders Ahly, who beat Ittihad of Alexandria 1-0, have 31 points with a game in hand on Zamalek and Pyramids.
Early goals
Bencharki put Zamalek ahead after four minutes, planting a first-timer into the bottom corner after some good work from El-Said, who got the better of Burkina Faso midfielder Blati Toure on the left and crossed low for the Moroccan.
Pyramids keeper Sherif Ekramy also palmed away a curling shot from Bencharki before Zamalek retreated into their own half, allowing their opponents to assume control.
Veteran playmaker Abdallah El-Said had a close-range shot blocked by Zamalek keeper Mohamed Abou-Gabal following a superb back-heel flick from Ibrahim Adel before Pyramids deservedly levelled on 12 minutes.
Gabr, an ex-Zamalek defender, leaped above Hamza Mathlouthi at the far post to head home a corner before Pyramids went 2-1 up in the 34th minute after some slack marking.
A low cross from the lively Adel caught Zamalek's Mahmoud El-Wensh and Tarek Hamed ball-watching, with Lakay found unmarked to fire a low shot underneath Abou-Gabal and into the net.
El-Said equalised just before halftime after finishing off a superb move, finding the bottom corner with a bicycle effort following a first-time cross from Ashour.
Ashour, who has also become a key member of Egypt's squad recently, netted what proved to be the winner eight minutes into the second half, drilling an angled low shot into the far bottom corner after Bencharki laid the ball for him on a plate.
Zamalek had defender Mahmoud El-Wensh dismissed for a second bookable offence in stoppage time but it mattered little as the team held on for a valuable victory.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)
Short link: