So even domestic flyers (whether Moroccan residents or foreigners) need to go in and out through immigration?
This sort of situation existed with
https://en.wikipedia.org/wiki/Qantas_Flights_7_and_8 when the range of the aircraft used was insufficient to get from the US to SYD, requiring refueling in BNE (and occasionally somewhere else in the Pacific).
You could book a ticket for just BNE to/from SYD, but this would require using the international terminals (all other domestic flights use completely separate from terminals and less stringent security checks, for example no liquid restrictions), and you'd get some sort of pass to go through immigration at both ends, there's probably a trip report about it on here.
As for being savvy, the only way to know is to read other people's experiences before your trip.... and if you don't feel you were scammed, the scammers did a very good job!
Moroccan currency is restricted so not usually accepted once you have passed through immigration, and airport money changers are (almost) always extortionate - that you should have known in advance.