A Moroccan army defeated the Songhai in 1591 and made Timbuktu, rather than Gao, their capital. The invaders established a new ruling class, the Arma, who after 1612 became virtually independent of Morocco.
Timbuktu Trivia: Unveiling the Mysteries of the Golden City