Against the run of play, Khaled Korbi gave the Tunisians a 1-0 lead when his in-swinging free kick evaded everybody in the penalty area on its way into the bottom corner.
Queens Park Rangers midfielder Adel Taarabt was introduced five minutes into the second period and was immediately involved in the action.
The playmaker dribbled into the area before shooting against the arm of a Tunisian defender, but the referee waved away Moroccan appeals for a penalty.
Tunisia doubled their advantage courtesy of Youssef Msakni. The substitute ran into the box before sending a deflected shot low into the net.
Houcine Kharjah set up an interesting finish when he pulled a goal back for Morocco four minutes from time.
Taarabt had the best chance to equalise but headed over the bar as the 2004 champions hung on to claim the three points.