Striker Amr Marei put Masry ahead after 10 minutes at Alexandria's Borg El-Arab Stadium when he broke clear following a flick from a teammate before firing a low shot home.
However, it did not take Berkane too long to equalise, with Hamza Regragui drilling home following a low cross from Congolese winger Chadrack Lukombe seven minutes later.
Masry's hopes of restoring their advantage were dealt a huge blow in the second half when Amr Moussa and Tunisian midfielder Elyes Jelassi received straight red cards following VAR reviews.
Moussa was sent off in the 50th minute for stamping on the foot of an opponent following a reckless tackle before Jelassi followed him down the tunnel for an even uglier and deliberate challenge, stamping on the knee of a Berkane defender in an off-the-ball incident.
But the visitors failed to take advantage and were stunned with eight minutes remaining when Layouni curled a wonderful, long-range free-kick over the wall and into the net to spark wild celebrations in the stadium.
Masry will be hoping to protect their advantage when they face Berkane away from home in the second leg next Sunday.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)
Short link: