Why (political intention) they did it?
Primarily, there are two interrelated factors here.
The first was the marriage of King Mohammed VI of Morocco that was occurring at the same time as the "invasion"
The invasion was greeted with amazement by Moroccans, who were yesterday readying themselves for three days of celebrations to mark King Mohammed VI's recent marriage. Most had not even realised that the island, so close to the popular beaches north of Tangiers, was meant to be Spanish.
The invasion, which appeared deliberately timed to coincide with the wedding celebrations, provoked jitters in the Spanish government and the EU.
This appeared to be an attempt by the newly-married king to force the issue of discussing the status of the more significant cities, Ceuta and Melilla, that Spain controls on the African continent on the Mediterranean coast within Moroccan border. It would seem that the king wanted to show "he meant business" about recuperating these cities for Morocco and resolving issues related to fishing rights, illegal immigration, and the status of Western Morocco, which was under dispute at the time.
But although the islet has returned to Spanish control, the tensions that lie behind this odd confrontation have not been addressed. Morocco has long complained about Spain's sovereignty over Ceuta, a city on Morocco's North African coast that is home to 70,000 Spaniards, many of Moroccan descent. Melilla, another Spanish enclave on the Moroccan coast, stirs a similar conflict.
The two nations also struggle over issues of illegal immigration, fishing rights and the future status of Western Sahara, which Morocco occupies in defiance of a demand for independence from an indigenous people, the Saharawi.
Interestingly enough, there has been a movie made about what happened, called "La Isla" (2016) from the Moroccan standpoint, though not a literal retelling of the story. It may give some insight as to the Morrocan view toward the event. (This is not to be confused with the Argentinian movie of the same name about an invasion of another island off the coast of that South American country.)
For insight on the Spanish point of view, take a look at this article, which describes the national viewpoint of Spain and its response. It also briefly touches on some global aspects of the incident.