So even domestic flyers (whether Moroccan residents or foreigners) need to go in and out through immigration?
Yes, whether by train, plane or automobile. Or so I've read here and elsewhere. I don't know the current state. But anyone can google around and find sites like...
https://www.onda.ma/Je-suis-Passager...-R%C3%A9ponses