Considerations on the Origins of Gnosticism
The origins of Gnosticism have been long disputed, and the conclusions reached have usually been unsatisfactory. I will not attempt to describe these theories or to refute them here. Rather I would like to present a possible solution to this problem.
Scholars tend to argue that Gnosticism arose in the second century. There are some contraindications to this view. In the first place, the Gospel of Thomas, which has elements often characterized as Gnostic, may well date back to the mid-first century. As a sayings collection, it may predate the canonical Gospels. Scholars who have dated it to the second century appear to do so mostly through a kind of circular reasoning. Thomas, they say, has Gnostic elements, and since Gnosticism did not arise till the second century, it must therefore be Gnostic. But this is a kind of petitio principii: you cannot date Thomas to the second century simply because of this premise, because if Thomas is Gnostic, Gnostic elements must go back to the first century. The claim that it is from the second century is part of an assumption that you are using to prove itself.
In any event, there are elements that can be called Gnostic in the New Testament. One obvious example is Ephesians 6:12, which speaks of the struggle "against principalities, against the rulers of the darkness of this world, against spiritual wickedness in high places." The author of this text is explicitly saying that the struggle is not against the Roman civil authorities, but against malign higher powers that a Gnostic could easily equate with the archons.
Even so, it is fair to say that Gnosticism in its classic form appears to be a product of the second century. But why would it have come about?
In the following I am going to speak of "Christians" in the sense of the Christians who wrote the New Testament and the views that they more or less consistently represent. Evidently there were many other types of Christians from the very earliest days. This fact can be most easily explained by saying that Christ's own disciples understood his teachings in quite different ways; he may have even taught them different things according to their capacities. But of those other Christianities we can say little. They left few if any accounts (or if they did, these texts were lost or destroyed) and did not leave any spiritual descendants that survived the end of classical antiquity.
So when I say "the Christians" here, I am speaking of the Christians that we can know something about. The New Testament states their theology quite clearly (if one looks past attempts to father later, quite alien dogmas upon them). They believed from a very early point on that Jesus Christ was more than an ordinary human. Rather he was the incarnation of the Great Angel, Metatron, whom Philo describes as "the archangel of many names": "And even if there be not as yet any one who is worthy to be called a son of God, nevertheless let him labour earnestly to be adorned according to his first-born word, the eldest of his angels, as the great archangel of many names; for he is called, the authority, and the name of God, and the Word, and man according to God's image, and he who sees Israel."  One of these names was the Son of Man (inspired no doubt by Daniel 7:13). He was even sometimes called a deuteros theos—a "second God" whose presence is remarkable in a faith as rigorously monotheistic as ancient Judaism.
The actual relation of Jesus to this Great Angel appears to have been a matter of debate. Scholars have proposed that Mark represents an adoptionist view of the Incarnation: that the Gospel begins with the baptism of Jesus because Mark believed that this was when Jesus was, as it were, overshadowed by the Son of Man. Certainly Mark's Jesus speaks of the Son of Man as himself. Matthew and Luke, with their nativity accounts, appear to hold that Jesus was the incarnation of this Son of God from the time of his conception. (Speculating rather wildly, one might even imagine that they wrote their Gospels to counter the adoptionistic views of the earlier Mark.)
In any event, the early Christians—again I mean the Christians who wrote the New Testament—believed Jesus was the incarnate Son of God, the Son of Man. Furthermore, they believed that Satan, the Devil and his minions were the "rulers of the spiritual wickedness of this world." The Law had been given to the Jews as a kind of provisional measure in order to enable to live them decently under this wicked system, but with the coming of Jesus, the Law was either unnecessary or at any rate not as important as it had been.
Indeed the coming of Jesus, according to this view, broke the power of Satan in this world. Sometimes this act is portrayed as predating the passion of Christ, as in Luke 10:17: "I beheld Satan fall as lightning from heaven" But increasingly the Christians that Christ's death and resurrection had broken Satan's power. Exactly how this was believed to have happened was not entirely clear—the various doctrines of the Atonement would not be articulated till centuries later—but it is most clearly articulated in an agraphon of Jesus found in the Codex Bezae (but which Jerome said appeared in many Greek codices of his day) after Mark 16:14, in which the risen Jesus appears to upbraid the disciples for "their unbelief and hardness of heart, because they believed not them which had seen him after he had risen." The addition in the Codex Bezae reads:
And they excused themselves, saying, "This age of lawlessness and unbelief is under Satan, who does not allow what lies under the unclean spirits to understand the truth and power of God. Therefore reveal thy righteousness now"—thus they spoke to Christ. And Christ replied to them, "The term of years for Satan's power has been fulfilled, but other terrible things draw near. And for those who have sinned I was delivered over to death, that they may return to the truth and sin no more: that they may inherit the spiritual and incorruptible glory of righteousness which is in heaven." 
This is hardly the place to go into the vexatious subject of the ending in Mark, but it is tempting to believe that this was the original ending (removed for reasons that I will go into below). In any case, this passage reflects the theology underlying the New Testament. Christ had broken the power of the Devil, and the final judgment was due very soon. The Apocalyptic Discourse of Matthew 24, Mark 14, and Luke 21 forecast the invasion of Judea and the sack of the Temple by the Romans, to be shortly followed by the return of the Son of Man and the final judgment.
Revelation portrays this concept graphically. It appears to be saying this: the sacrifice of Christ constituted a purge of the heavenly realms, first the spheres of the seven planets (hence the relentless repetition of the number seven.) Finally "the great dragon was cast out, that old serpent, called the Devil, and Satan, which deceiveth the whole world: he was cast out into the earth, and his angels were cast out with him" (Revelation 12:9). The Devil was not to be cast out of heaven at the Last Judgment—the Devil had already been cast out, and he had taken the form of the "beast" (Revelation 17:8): the Roman Empire, built on seven mountains (Rome was built on to seven hills). Rome has "seven kings; five are fallen, and one is, and the other is not yet come" (Revelation 17:10). If, as is usual, we take the seven kings to refer to the seven emperors, counting from Augustus to Tiberius, Caligula, Claudius, and Nero, we have the "five fallen"; the sixth, Revelation implies, is still reigning, which brings us to Galba, ruling briefly in 68-69, to be quickly replaced by Otho, who would himself reign only three months.
To the author of Revelation, the time of this beast is short: the Word of God will come and cast the beast "alive into a lake of fire burning with brimstone" (Revelation 19:10). In short, Revelation is writing about a time when the seven seals have been opened—that is, the seven planetary spheres, infested with evil spirits, have been purged—and Satan has been cast down onto earth. He takes the form of the Roman Empire, and Christ is imminently due to return and crush him.
This consideration leads us to date Revelation to the tumultuous time of the Jewish War in the late 60s. Certainly the text's febrile mood suits this period much better than it does the relatively calm period of the mid-90s, to which, on the authority of Irenaeus, Revelation is usually dated. (As Margaret Barker has suggested in her book The Revelation of Jesus Christ, Revelation, or part of it, may have originally been written in Aramaic during this period, and only completed and/or translated into Greek in the mid-90s).
In any event, the synoptic Gospels and Revelation all point to a belief that the Jewish War was a preamble to the Second Coming of Christ and the final judgment of the world. (Note that John's Gospel, conventionally dated to the last decade of the first century, makes no such assumptions. It has no Apocalyptic Discourse, and it points to a Christ who is not about to return immediately in visible form but who is mystically present among his followers.)
The Jewish War came. Given the Jews' intense hatred of Rome, one could easily have predicted that their rebellion was imminent even decades in advance, and given the power of Rome, one could easily have foreseen the outcome. Judea was ravaged, the Temple was sacked, and the Jews and Christians were both dispersed. But the world did not end: cats, as the saying goes, still had kittens. Rome was not crushed by the legions of Christ: the seventh "king," Otho, was rapidly replaced by Vitellius, who in turn would be immediately ousted in favor of Vespasian—the commander of the Roman forces in the Jewish War—and the empire settled down to normal.
The failure of Jesus to return immediately must have convulsed Christian theology. Many must have agreed with the opinion stated in 2 Peter 3:4: "Where is the promise of his coming? for since the fathers fell asleep, all things continue as they were from the beginning of creation." The same epistle (dated to the early second century) presents the position that Christianity would adopt: "The Lord is not slack concerning his promise, as some count slackness; but is longsuffering to us-ward, not willing that any should perish, but that all should come to repentance" (2 Peter 3:9).
The rest of the history of Christian eschatology has been laid out often and elaborately. But one detail has been omitted: what about the claim that Satan had been thrown from the heavens? We can see how Revelation was subsequently treated: although its author was undoubtedly writing about current events—the imperial upheavals of the late 60s and the Jewish War pointing to an end that he believed was already at hand—later interpreters had to postpone the date of this final resolution to some indefinite (but invariably close) point in the future.
Even so, some of the texts reflected the original belief that Satan had already been cast down from heaven, and yet, as the detractors say in 2 Peter, "all things continue as they were from the beginning of creation." Conceivably the passage from the Codex Bezae quoted above was the original ending of Mark but was removed because it too explicitly claimed that Satan had been cast down. Luke 10:18, by contrast, would have been permitted to survive because it was too enigmatic to present exegetical difficulties.
Christian theology, then, came up with its well-known, although ambiguous and confusing, answer: the power of Satan has indeed been broken, except that it is not quite over yet. This awkward position was created to try to understand the message of the New Testament in the light of eventualities that by no means turned out as predicted. One could assume that this position had solidified by the end of the first century, when protocatholic Christianity was taking shape.
But what if some Christians came up with a different interpretation? What if they said that Satan was really the god of this world because he had created this world, whose shortcomings are both obvious and excruciating? And that the way out of it was not through some Last Judgment that was never going to come, but through inner illumination?
This hypothesis could explain where Gnosticism came from. In its classic, full-blown form, it did arise in the second century, as another response to certain necessary deviations from the original Christian message. As I have said, some elements that we now consider Gnostic were present in first-century Christianity: Paul speaks of "the wisdom of God in a mystery, even a hidden wisdom" (1 Corinthians 2:7) that was very likely quite different from the later dogmas of exoteric Christianity. But these elements were only fully synthesized in the second century, with the great Gnostic theorists such as Valentinus and Basilides.
 Philo, On the Confusion of Tongues, 28.146.
 Translation quoted from Bruce M. Metzger, The Text of the Greek New Testament: Its Transmission, Corruption, and Restoration, 3d ed. (Oxford: Oxford University Press, 1992), 57. I have adopted a variant reading in his footnote because it is closer to the original Greek. This text can be found as a footnote to Mark 14:14-15, in The Greek New Testament, 3d ed. Kurt Aland et al. (N.p.: United Bible Societies: 1983).
 Irenaeus, 5.30.3. Irenaeus himself may have been saying something different. For an interesting interpretation of this passage, see Joel Griffis, "Irenaeus and the Date of Revelation," The Ironclad Network website, Jan. 21, 2017.