The two have connections with foreign extremists and were planning to receive military training in IS camps before carrying out attacks in Morocco, the ministry said in a statement on Friday.
One of them had close relations with a criminal group in Fez city. The detentions led to the arrest of four more people in connection with the gang.
Morocco is facing a "serious terrorist threat" linked to the growing number of people joining jihadi groups in the Middle East.
Interior Minister Mohamed Hazard said in July that around 1,120 Moroccan nationals and some 2,000 of Moroccan origin have been enrolled in terrorist groups in Syria and Iraq.