Haythem Layouni scored direct from a free-kick on 82 minutes to give the home team a narrow aggregate lead after a match that kicked off late on Sunday and finished past midnight.
Tunisian Layouni curled a long-range kick over a defensive wall and past goalkeeper Hamza Hamiani Akbi to put Masry ahead for the second time in an incident-packed match.
Amr Marey gave Masry an early lead by sprinting on to a headed pass and controlling the bobbling ball to fire into the net from close range.
Berkane, the 2020 Confederation Cup winners, equalised within seven minutes as Hamza Regragui tapped a low cross from Democratic Republic of Congo-born Chadrack Lukombe over the line.
Ill discipline cost Masry dearly in the second half, leading to red cards for midfielders Amr Moussa and Tunisian Elyes Jlassi and leaving the hosts having to play 25 minutes with nine men.
In each case the referee initially showed a yellow card, only to upgrade to red having watched replays of the incidents on a touchline VAR monitor.
Berkane winger Youssef el Fahli was the victim in both incidents at the 87,000-capacity Borg el Arab Stadium in the ancient Mediterranean city of Alexandria.
Moussa was guilty of a late, studs-up tackle while Jlassi stamped on the right leg of El Fahli after the Moroccan had fallen over.
– Woodwork foils Pyramids –
The best Confederation Cup showing by Masry was in 2018 when they reached the semi-finals before losing heavily to V Club from DR Congo.
Pyramids, the other Egyptian title contenders in the African equivalent of the UEFA Europa League, were foiled twice by the woodwork in a 0-0 home draw against TP Mazembe from DR Congo.
Although five-time African champions Mazembe spent much of a cagey clash in Cairo defending, they also had chances, notably when Zemanga Soze forced a fine save from veteran goalkeeper Sherif Ekramy.
The draw has tilted the odds toward overall victory for Mazembe, whose 11 CAF titles include two in the Confederation Cup.
Mazembe boast a perfect record in Lubumbashi this season and have not conceded in four previous home matches in the competition while Pyramids have failed to win in three of five away games.
A Libyan showdown between fierce Tripoli rivals Al Ittihad and Al Ahly ended 0-0 with the match moved to Benghazi because no stadium in the capital city meets international standards.
On Sunday, Shomari Kapombe converted a 68th-minute penalty in Dar es Salaam to earn Simba of Tanzania a 1-0 win over 2015 Confederation Cup runners-up Orlando Pirates of South Africa.
All four return matches are scheduled to kick off on Sunday, but two will finish after midnight because many north African players are observing a dawn-to-dusk fast during the Muslim holy month of Ramadan.
The two-leg semi-finals and single-match final, at a venue to be announced, are scheduled for May with the winners pocketing $1.25 million (1.1 mn euros).