From the 3rd century AD, the Berbers established trading routes all over the Western Sahara, including in Mauritania. In the 11th century, the Marrakesh-based Islamic Almoravids pushed south and, with the assistance of Mauritanian Berber leaders, destroyed the Empire of Ghana, which covered much of present-day Mauritania. That victory led to the spread of Islam throughout Mauritania and the Western Sahara. The descendants of the Almoravids were finally subjugated by Arabs in 1674.

As colonialism spread throughout Africa in the 19th century, France stationed troops in Mauritania, but it was not until 1904 that, having played one Moorish faction off against another, the French finally managed to make Mauritania a colonial territory. Independence was fairly easily achieved in 1960 because the French wanted to prevent the country from being absorbed by newly independent Morocco. Mokhtar Ould Daddah became Mauritania's first president.

Ould Daddah took a hard line, especially against the (mainly black African) southerners, who were treated like second-class citizens and compelled to fit the Moors' mould. Any opposition was brutally suppressed.

The issue of Western Sahara (Spanish Sahara) finally toppled the government. In 1975 the very sandy Spanish Sahara (a Spanish colony) was divided between Morocco and Mauritania. But the Polisario Front launched a guerrilla war to oust both beneficiaries from the area. Mauritania was incapable, militarily and economically, especially in the midst of terrible droughts, of fighting such a war. A bloodless coup took place in Mauritania in 1978, bringing in a new military government that renounced all territorial claims to the Western Sahara.

A series of coups ensued. Finally, Colonel Maaouya Sid' Ahmed Ould Taya came to power in 1984. For black Africans, this led to even worse conditions than under Ould Daddah. Ethnic tensions culminated in bloody riots between the Moors and black Africans in 1989. Around 100,000 Mauritanians were expelled from Senegal and more than 70,000 black Africans were expelled to Senegal, a country most had never known.

In 1991 Mauritania supported Iraq during the Gulf War, and aid dried up. To counter criticism, Taya introduced multiparty elections in 1992, which were boycotted by the opposition. Riots over the price of bread in 1995 worsened the political situation. Cosmetic elections were held in 2001, with Taya still holding the whip hand; opposing political parties and Islamists were deemed threats to the regime and both were repressed.

The 2000s were marked by more instability. In June 2005, Taya was toppled in a bloodless coup led by Colonel Ely Ould Mohamed Vall. Vall was popular with many in Mauritania; he formulated a new constitution and voluntarily gave up power by holding elections in March 2007. Sidi Ould Cheikh Abdallahi was returned as Mauritania's first democratically elected president. He openly condemned the 'dark years' of the late 1980s, and sought rapprochement with the expelled black Moors – a move that angered the traditional elites and which led, in part, to his overthrow by General Mohamed Ould Abdel Aziz in a coup in August 2008. Despite international condemnation, Aziz's position was consolidated the following year in elections that saw him narrowly returned as president.

Initially, under Aziz, the economy grew, in part due to mineral extraction, gas exploration and new factory fishing licences given to EU and Chinese fleets. However, a prolonged drought in 2011 led to rocketing food prices and an increase in aid dependency for swathes of the population, while several al-Qaeda affiliated attacks between 2008 and 2011 decimated an already fragile tourism industry. The second most influential political party, at least in terms of parliamentary representation, has been the reformist Islamists.