Originally a Roman port called Portus Divinus, Mers-el-Kébir became an Almohad naval arsenal in the 12th century, fell under the rulers of the Kingdom of Tlemcen in the 15th century, and eventually became a center of pirate activity around 1492. It was occupied variously by the Ottoman Turks, Portuguese and Spanish, with the Spanish (who named it Mazalquivir) capturing it in 1505 under Cardinal Cisneros. Mazalquivir was the base used to capture neighbouring Oran in 1509. The Spanish held both cities until 1708, when they were driven out by Turkish Bey Mustapha Ben Youssef (also known as Bouchelaghem). The Spanish returned in 1732 when the armada of the Duke of Montemar was victorious in the Battle of Aïn-el-Turk and again took Oran and Mazalquivir. Both cities were held until 1792, when they were sold by King Carlos IV to the Dey of Algiers.
Following the German defeat of France in 1940, part of the French fleet holed up in the port. On 3 July 1940, Force H of the British Royal Navy, led by Vice Admiral Sir James Somerville, attacked the French Navy's fleet at Mers-el-Kébir, killing more than 1,200 men, with the goal of preventing it from falling into German hands.
After World War II, France used its naval base at Mers-el-Kébir to assist in atomic tests. The Évian Accords of 18 March 1962, which recognized Algerian independence, allowed France to maintain its base for 15 years; however, France withdrew five years later in 1967.