NOUAKCHOTT (Reuters) - A Mauritanian blogger condemned to death in 2014 for apostasy in a Facebook post about Islam will be freed because an appeals court on Thursday overturned his sentence, his lawyer said.

Mohamed Ould Cheikh Ould Mkhaitir has spent nearly four years in jail after a high-profile case surrounding a post in which he criticized the use of religion to justify social discrimination.

The case has highlighted tempestuous social divisions in the desert West African country, often based on ethnicity. Thousands protested in the capital Nouakchott and other cities during the trial demanding Mkhaitir be put to death.

Mauritania has not carried out a death penalty sentence since 1987, but last year a group of influential Muslim clerics urged authorities to apply the harshest punishment.

The court reduced Mkhaitir's sentence to two years in prison and a fine of 60,000 ouguiyas ($170). He will be allowed to go free because of time already served in prison, his lawyer Me Mohamed Ould Moine told Reuters.

"This is a great victory for Mauritanian justice," Moine said after the appeal hearing in the northern city of Nouadhibou. "The judges respected Mauritanian law taking into account his regrets and repentance."

The streets of Nouakchott were calm immediately after the hearing, though there was extra security in place there and in Nouadhibou. His lawyer and rights groups have expressed concerns about Mkhaitir's safety following death threats to him and his friends.

(Reporting By Kissima Diagana; Writing by Edward McAllister; Editing by Matthew Mpoke Bigg)