An ellipsis (anapodoton) of "If the mountain won't come to Muhammad then Muhammad must go to the mountain."
The earliest appearance of the phrase is from Chapter 12 of the Essays of Francis Bacon, published in 1625:
Mahomet made the people believe that he would call a hill to him, and from the top of it offer up his prayers, for the observers of his law. The people assembled; Mahomet called the hill to come to him, again and again; and when the hill stood still, he was never a whit abashed, but said, If the hill will not come to Mahomet, Mahomet will go to the hill.
It was published in John Ray's 1670 book of English proverbs, , Though widely attributed to Muhammad, the prophet of Islam who lived in Arabia in 6th century, there is no written or oral tradition that traces this phrase back to him.