Marlowe probably took the twenty-four years from the 1587 work Historia von D. Johann Fausten, commonly called the Faustbuch. This was published in Frankfurt, in the German language, and frequently reprinted. It was translated into English as The Historiye of The Damnable Life and deserued Death of Doctor Iohn Faustus in 1592, a text now lost, although we do have later versions than those that might have been available to Marlowe.
In the 1618 version of the English text, chapter 6, Faustus promises as follows:
at the end of 24. yeares next ensuing the date of this present letter, they being expired and I in the meane time, during the said yeares be serued of them at my will, they accomplishing my desires to the full in all points as we are agréed: that then I giue them all power to doe with me at their pleasure, to rule, to send, fetch, or carry me or mine, be it either body, soule, flesh, blood, or goods, into their habitation, be it wheresoeuer: and hereupon, I defie God and his Christ, all the hoast of Heauen, and all liuing creatures that beare the shape of God, yea all that liues: and againe I say it, and it shall be so.
The text is clearly similar to Marlowe's version of the pact, which also mentions "full power to fetch and carry the said John Faustus, body and soul, flesh, blood, or goods, into their habitation". This compares to the German original:
daß so 24. Jahr, von Dato diß Brieffs an, herumb und füruber gelauffen, er mit mir nach seiner Art und weiß, seines Gefallens, zuschalten, walten, regieren, führen, gut macht haben solle, mit allem, es sey Leib, Seel, Fleisch, Blut und gut, und das in sein Ewigkeit. Hierauff absage ich allendenen, so da leben, allem Himmlischen Heer, und allen Menschen, und das muß seyn.
Both texts then go to a year-by-year narrative of Faustus's adventures. At the seventeenth year, he signs a further letter acknowledging that there are only seven to go. In his final speech he mentions the twenty-four years several times, saying:
I haue promised vnto him at the end and accomplishing of 24. yeares, both body and soule, to doe therewith at his pleasure: and this day, this dismall day, those 24. yeares are fully expired, for night beginning, my houre-glasse is at an end
This is all to say that Marlowe's primary source repeatedly mentions this specific term. The Faustbuch's origins are obscure but it may draw on the Christlich bedencken of Hermann Witekind (writing under the name Augustin Lercheimer), a contemporaneous work on the topic of witchcraft. There is a later edition in 1597 that responds to the Faustbuch and says it introduced errors into the story! The 1585 text does mention a pact of twenty-four years, though, and a separate passage narrates the re-making of the agreement (which the Faustbuch puts in year 17 specifically).
Witekind's text relates the Faust legend to witchcraft and witch-trials in general. In that line, the general idea of making a pact with the devil is well-known. The notion of a fixed term is probably because in the theory of dealing with demons, you are meant to specify a length of time that the demon will be bound (and in general you are supposed to be very precise about all the contractual terms). Moreover, immortality may not be within Mephistopheles's power to give. So the metaphysics of the situation demand that Faustus's soul be forfeit after some finite amount of time, although there is nothing in the broad background to say why 24 years might be a special number.
One theory from John Henry Jones demands a little more calculation (in his introduction to William Empson's Faustus and the Censor, 1987). The supposed historical Faust died in 1541. Twenty-four years before that is 1517. A very notable event of 1517 is that on 31 October, All Hallows Eve, Martin Luther nailed up his Ninety-Five Theses on his church door in Wittenberg. While the historical Faust was associated with all sorts of locations, the Faustbuch Faust is located squarely in Wittenberg, as is Marlowe's Faust. There is therefore a parallel between Luther and Faust making a significant decision to sign a document in 1517. Jones supposes that there is a hidden anti-Lutheran point being made. (He also says he is unaware of any other devil-pact of 24 years specifically, saying that 25 or 50 are more usual.)
There are several other connections between early versions of the Faust story, and early Protestantism. Johannes Manlius, a student of Reformation theologian Philip Melanchthon, claimed in a 1562 work that Faust was from a nearby town to Melanchthon. The Faustbuch version, and Marlowe, also have him pranking and striking the Pope; in various texts he throws lavish parties on fast-days; and the entire legend interacts with notions of sin and redemption which are theological flash-points. All of this makes it somewhat plausible that in the many retellings, happening at a time of great religious turmoil, the connection to 1517 is not an accident.