For the most part, the answer boils down to trade.
From around the late 600s to the 1600s, there was a fairly large Trans-Saharan trade network between the people of North Africa (generally from Morocco through Tunisia, though those nations didn't necessarily exist in the same form as today) were rich in salt, while the people of West Africa, especially the Mali Empire, were rich in gold.
The people of North Africa, most notably the Berbers, were Muslims, residing lands conquered by the Islamic Caliphate. The people of sub-Saharan West Africa were mainly animistic at first. However, trade was a major facilitator for the spread of Islam. Many sub-Saharan merchants would convert to strengthen their commercial connection to the Muslims. From there, Islam was able to spread throughout other classes and gain more influence.
In east Africa, Islam spread through the Horn of Africa (modern-day Ethiopia, Somalia, etc.) and the Swahili city-states (Kenya, Tanzania, Mozambique) through trade as well. There were numerous trade routes throughout the Indian Ocean, connecting the Swahili city states towards the southern east coast of Africa with as far east as China. Goods and ideas spread through these vast trade networks, and the Middle East/Arabian Peninsula was kind of at the center of it all. Much like in West Africa, this trade would prompt many merchants to convert, and from there Islam grew in influence. This trade network is also the reason Indonesia today is the country with the largest Muslim population.
Sources:
Edward William Bovill - The Golden Trade of the Moors: West African Kingdoms in the Fourteenth Century
Derek Nurse - The Swahili: Reconstructing History and Language of an African Society, 800-1500