The justice ministry said the 5,654 detainees that would be freed were selected based in their age, health, good conduct and length of detention.
They would be released in stages due to “exceptional circumstances linked to the emergency health situation and necessary precautions” against the virus, it said in a statement.
The decision came as the COVID-19 illness has officially killed 66 people and infected 960 in the North African country.
The king also ordered authorities to take “all the necessary measures to reinforce the protection of detainees in prisons” which are widely known to be overcrowded.
Morocco, home to 35 million people, has an estimated 232 detainees per 100,000 inhabitants.
With the spread of the coronavirus pandemic, rights groups have urged authorities to release all political prisoners, including those held in preventive detention or nearing the end of the terms.
But it was not immediately clear if detainees linked to the “Hirak” protest movement that rocked northern Morocco in 2016 and 2017 would be among those pardoned by the king.
Morocco’s prison authorities have meanwhile taken measures to protect staff and reduced visits.
The UN rights chief Michelle Bachelet last month said countries should consider releasing older detainees and low-risk offenders.
Several other countries, including Iran, Afghanistan and Indonesia, have moved to release thousands of prisoners to lower the risk of a major outbreak of the virus in prisons.