The Prophet Muhammad (pbuh) was invited to Madinah to unite two tribes. He came with about 300 Muslims. During the next 10 years, until his (pbuh) death, most of the arabian peninsula had accepted him as the Prophet of God.
While in Madinah, the Quran revelaed to the change of direction from Jerusalem to Makkah. Someone were praying here, when others told them to turn around. This is the Mosque of the two Qiblas (directions).