Julius Honorius, a Roman writer of Late Antiquity (c. 500 +/- 50 years), was apparently the first to describe the pyramids as Joseph’s granaries, in his Cosmographia, in which he says that the pyramids “are called the storehouses of Joseph,” but without elaboration. The claim appears again in the commentaries of Pseudo-Nonnus in the first half of the 500s. Gregory of Tours, who in 594 CE wrote in the History of the Franks 1.10 that Joseph’s granaries were made of stone, wide at the base, and narrow at the top. Although he had never seen the pyramids, they are clearly his inspiration. In 825 CE the monk Dicuil, writing in the Liber de Mensua Orbis Terrae 6.13, described the monk Fidelis’s visit to the pyramids and identified them as Joseph’s granaries. The claim appears as well in the commentaries of Nicetas of Heraclea in the eleventh century and the Byzantine Etymologicum magnum of the twelfth century (among other sources), in both of which the word “pyramid” is said to derive from the Greek word for “grain.”
The reason for this belief is a little unclear. Some of it is likely due to sheer ignorance at the end of Antiquity, when Egypt was slowly falling out of the increasingly isolated West’s orbit. Although the Byzantines had the legend, it was never as popular in the East, where Classical views of the pyramids in Greek competed with Christian views. The oldest Islamic attestation of the granaries myth that I know of is Al-Idrisi’s History of the Pyramids (c. 1150 CE), which was likely reporting it from a Christian source; however, I have read that earlier Islamic authors dismissed the granaries claim as unfounded. Prior to that, Islamic lore generally considered the pyramids to be antediluvian structures, or at least vastly ancient, and the storehouses to be much more recent.
Another reason is probably cultural appropriation. Reassigning the pyramids from pagan Egyptian tombs to holy granaries of a Biblical patriarch Christianized them and made them an acceptable monument to the Judeo-Christian heritage in the years when Christianity finally overcame paganism in Byzantine Egypt.
But the clearest and best explanation is an inference that can be found in our friend Rufinus, who reported in Ecclesiastical History (his translation of Eusebius) 11.23 that Christians and Jews in Egypt alike both identified Joseph with the Greco-Egyptian god Serapis, a form of Osiris, and the Jews said that a statue of Serapis the grain-giver actually depicted Joseph. This claim can be found as far back as Tertullian, in Ad nationes 2.8 (197 CE): “For that Serapis of yours was originally one of our own saints called Joseph” (trans. Peter Holmes). Thus, some scholars have argued that the sarcophagus of the Apis bull (in this period, an aspect of Serapis) became identified with the sarcophagus of Joseph, and both Joseph and Osiris-Serapis were said to have had their coffins drowned in the Nile (the former in a Jewish tradition repeated by Christians and Muslims). But what is most relevant is from this is that because the pyramids were known to be tombs, and the Late Antique Egyptians associated death with Serapis, the inference is that pyramids were seen as the realm of Serapis. Thus, some scholars have concluded that for Christians and Jews, these became the structures of Joseph, and since Osiris-Serapis was identified with the grain in Egypt (as Plutarch reported in Isis in Osiris), it’s a small inference to call the pyramids the place where the grain-giving Joseph operated.
Jason Colavito, “The Long, Strange History of the Pyramids as the Granaries of Joseph”, Jason Colavito Blog (6 November 2015) [http://www.jasoncolavito.com/blog/the-long-strange-history-of-the-pyramids-as-the-granaries-of-joseph]