When the Abbasids captured Damascus, one of the Umayyad princes escaped and made the long journey from there to Spain to found Umayyad rule there, thus beginning the golden age of Islam in Spain. Cordoba was established as the capital and soon became Europe's greatest city not only in population but from the point of view of its cultural and intellectual life.
The Umayyads ruled over two centuries until they weakened and were replaced by local rulers.
Meanwhile in North Africa, various local dynasties held sway until two powerful Berber dynasties succeeded in uniting much of North Africa and also Spain in the 12th and 13th centuries. After them this area was ruled once again by local dynasties such as the Sharifids of Morocco who still rule in that country.
As for Spain itself, Muslim power continued to wane until the last Muslim dynasty was defeated in Granada in 1492 thus bringing nearly eight hundred years of Muslim rule in Spain to an end.