Q. Why did Jews go back to Egypt when the Torah prohibited this (Deut. 17:16)?A. Not once but three times were Israel told not to go back to Egypt. The other two references are Deut. 28:68 and Ex. 14:13.
Maimonides says clearly, “It is permitted for a Jew to settle anywhere except in Egypt” (Hilchot M’lachim 1:1). But Jewish communities did settle there, and Maimonides himself lived in Egypt and was the court physician.
The Radbaz (1479-1573) says the ban applies only to permanent settlement; Maimonides, he argues, did not mean to stay but had to remain because of his court post. The Maharshal (17th century) says the ban operates only when the Jewish people are a free people in their own land; until then we have to live wherever we can.
Maimonides himself might have felt that being an eminent person in Egypt might allow him to influence the rulers to facilitate a Jewish return to Israel.