With 93% of its population being considered religious,Islam is the majority and constitutionally established state religion in Morocco. The vast majority of Muslims in Morocco are Sunni belonging to Maliki school of jurisprudence. The King of Morocco claims his legitimacy as a descendant of the Islamic prophet Muhammad.
The second-largest religion in the country is Christianity, but most Christians in Morocco are foreigners. There is a Bahá'í community. Only a fraction of the former number of Jews has remained in the country, many having moved to Israel.
The Moroccan constitution grants the freedom to worship and congregation, while recognizing Islam as the state religion. But the Moroccan penal code contains many laws that contradicts the constitution, including the 220, 222 articles of the penal code of the country, which are usually used against non-Muslim Moroccans. 
Islam reached Morocco in 680 CE, taken to the country by the Arab Umayyad dynasty of Damascus. The first Islamic dynasty to rule Morocco were the Idrissids, who were of the Zaydi Shia school. Article 6 of the Moroccan constitution states that Islam is official religion of the state. The King of Morocco claims his legitimacy as a descendant of the Islamic prophet Muhammad.
The Maliki Sunnite branch of Islam is dominant, while a minority belongs to Shiite branch. Relations between Sunni and Shiite have been strained in recent years, with a Moroccan crackdown on material and organisations originating from Shia Iran and the group Hezbollah.
Morocco first experienced Christianity while under Roman rule, as the Empire converted to the faith in its later years. Many of the pre-Christian religions were then reduced in number as Christianity spread. However, after the arrival of Islam, Christianity ceased to have a significant population in the country.
Due to the Spanish and French colonization beginning in the 19th century, Roman Catholicism grew in Morocco, albeit mainly being the European colonists. A small number of Moroccans with origins in these two countries remain in Morocco. The British, who mainly belonged to the Protestant Anglican Communion, were also given permission to build churches of their faith, such as the Church of Saint Andrew, Tangier.
Sub-Saharan Africans, mainly Catholics from former French colonies, have migrated to Morocco in recent years. Conversions of Moroccan Muslims to Christianity, mainly by American Protestants in the remote and mountainous south of the country, have taken place despite the risk of legal consequences. The CIA World Factbook estimates that Christians are currently 1% (~380,000) of the Moroccan population.
On 27 March 2010, the Moroccan magazine TelQuel stated that thousands of Moroccans had converted to Christianity. Pointing out the absence of official data, Service de presse Common Ground, cites unspecified sources that stated that about 5,000 Moroccans became Christians between 2005 and 2010. According to different estimates, there are about 25,000-45,000 Moroccan Christians of Berber or Arab descent mostly converted from Islam. Other sources give a number of a bit more than 1000. A popular Christian program by Brother Rachid has led many former Muslims in North Africa and the Middle East to convert to Christianity.
Morocco was a destination for the Jewish diaspora after the destruction of the Second Temple by the Roman Empire. A second wave of Sephardic Jews arrived in the country following the Alhambra Decree of 1492 which expelled all Jews from nearby Spain. The Jews, as well as the Christians, had legal autonomy relating to their own faith in cases when both parties were of the same religion.
After the creation of the Jewish state of Israel in 1948, the population of Moroccan Jews decreased significantly due to emigration. Moroccan Jews also migrated to other countries, such as the linguistically-similar France and Quebec, Canada. A total of 486,000 Israelis are of Moroccan origin, while the World Factbook estimates that in 2010 only around 6,000 Jews remained in Morocco. Most of them are elderly, with the largest population in Casablanca and the remainder thinly dispersed around the country.
The Baha'i Faith, which originated in the 19th century, is documented as starting its missions in Morocco in 1946, while the country was still under colonial rule. A Ten Year Crusade was initiated to spread the belief, establishing assemblies and schools in Morocco. In the early 1960s, shortly after independence, mass arrests were made of Baha'is, and death sentences given to the most prominent believers, sparking international outrage. Most estimates count the Baha'i population in modern Morocco as between 150 and 500.
Even though the Moroccan society remains very religious, irreligious people are significantly increasing in the recent years. In fact, about 7% of the Moroccan population are considered non-religious with 320 000 Moroccans being convinced atheists, and this number could be higher since irreligious Moroccans are usually in the closet in order to avoid troubles with their families and communities.
Despite the freedom given for Christians and Jews to practice their faith and congregate at places of worship, Islam is given rights by the constitution which outweigh those given to other faiths. For example, it is a criminal offence to possess a Christian Bible written in the Arabic language, part of a wider law prohibiting proselytisation of Muslims to any other belief.