Tunisian defender Haythem Ayouni combined with fellow countryman Elyes Jelassi to head home an early goal, which proved to be the winner.
Ahly hardly created any clear-cut chances, apart from a couple of opportunities in stoppage time. It was Masry who could have scored again, having stuck to their brave approach throughout.
The Port Said-based club claimed their first win over Ahly since 2016 to move up to sixth in the table with 19 points.
Ahly, whose four-match winning run came to an end, remain on top with 31 points, although Zamalek, who played a game more, moved level with them after beating Maqassa 1-0.
The Cairo giants will need to quickly pick themselves up before they meet Morocco's Raja in the first leg of the African Champions League quarters next weekend.
Masry surprisingly took the game to Ahly in the first half, with their illustrious opponents appearing uncharacteristically tame and vulnerable.
Mozambican winger Luis Miquissone, who scored the winner against Ittihad in Ahly's previous match, and South African forward Percy Tau were peripheral, failing to trouble Masry's comfortable defence.
Ahly created the first chance when a deflected shot from striker Mohamed Sherif went just wide after six minutes, but it was then a one-way traffic as Masry piled on pressure.
The hosts' pressure paid off two minutes later when Ayouni met Jelassi's inswinging cross with a powerful header from 12 yards that struck the hands of keeper Mohamed El-Shennawi and went in.
Winger Hassan Ali spurned a golden chance to double Masry's advantage in the 35th minute when he broke clear following a defensive lapse, only for El-Shennawi to thwart him with a superb one-handed save.
The second half was a scrappy affair, with Masry opting against putting men behind the ball and Ahly struggling for openings despite the introduction of talented winger Ahmed Abdel-Kader and striker Hossam Hassan.
Ahly could have snatched a draw in stoppage time though but a powerful drive from Amr El-Sulaya was saved by the keeper and a header from defender Yasser Ibrahim went just over the bar.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)