The main religion in Morocco is Islam, which is the state religion, however freedom of religious belief isn't always guaranteed to all. Officially, 99% of the population are Muslim, and virtually all of those are Sunni. Polls and surveys have found that 80–95% of its population is at least somewhat religious. The second-largest religion in the country is Christianity, but most Christians in Morocco are foreigners. There is a community of the Baháʼí Faith. Only a fraction of the former number of Jews have remained in the country, many having moved to Israel.
Main article: Islam in Morocco
According to The World Factbook maintained by the American Central Intelligence Agency, and a 2009 report by the Pew Research Center, about 99% of Moroccans are Muslims. Almost all of those are Sunni, although Pew reported in 2012 that when asked which sect they belong to, 67% of those surveyed answered Sunni, while 30% volunteered only that they were Muslim; none replied Shia. 50% of those who said they were Sunni did not believe that Shias were Muslims.
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 small minority belong to Zahirism or the Shiite branch. Relations between Sunni and Shiite have been strained in recent years, with a Moroccan crackdown on material and organisations originating from Shia-majority Iran and the group Hezbollah.
The Justice and Development Party is an Islamist party.
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 until finally being reduced to near extinction under the almohad's forced conversion policy. 
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 8,000–40,000 Moroccan Christians of Berber or Arab descent, mostly converted from Islam. Other sources estimated the number of convert to the Anglican Moroccan Church of a bit more than 1,000. 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 most recent estimates put the size of the Casablanca Jewish community at about 2,500, and the Rabat and Marrakesh Jewish communities at about 100 members each. The remainder of the Jewish population is dispersed throughout the country. This population is mostly elderly, with a decreasing number of young people.
Main article: Baháʼí Faith in Morocco
The Baháʼí 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 Baháʼís, and death sentences given to the most prominent believers, sparking international outrage. Most estimates count the Baháʼí population in modern Morocco as between 150 and 500. However Association of Religion Data Archives and Wolfram Alpha estimated 32,598 Baháʼís in 2005 and 2010.
Main article: Islam in Morocco
Survey results released in 2019 by Arab Barometer revealed that 13% of respondents who did the survey said they were not religious, compared to 82% who said they were, with 44% answering that they were somewhat religious, and 38% describing themselves as religious. Among those aged 18–29, only 24% said they were religious, compared to 68% of those aged 60 or more. Those without a university education were 20 points more likely to be religious than those with a degree. The proportion of religious respondents who were women was 44%, while 31% of men identified as such. There was no significant difference in religiousness between people in urban and rural areas.
A 2015 poll by Gallup International found that 93% of those surveyed said they were a religious person, 4% responded that they were not a religious person, while about 1% described themselves as "convinced atheists".
Main article: Freedom of religion in Morocco
The government plays an active role in determining and policing religious practice for Muslims, and disrespecting Islam in public can carry punishments in the forms of fines and imprisonment.
Only the religion of Islam is recognized by the Moroccan constitution as native to the country, with all other religions being considered "foreign". While foreigners can generally practice their religion in peace, citizens who practice "foreign religions" face obstacles from the government and social pressure. In particular, Shia Muslims and members of the Baháʼí Faith face discrimination from the government, as do some Christian groups.