According to Pliny the Elder, the first magical books were written by Osthanes, a Persian astrologer who accompanied Xerxes on his failed campaign to conquer Greece, and who left them behind in the retreat. However, all cultures capable of writing, whether on papyrus, clay, or bark, used that skill to record their own magical traditions; when they met other cultures, they traded. These books became known as grimoires, volumes of conjurations and spells, and their history is as old as civilization itself.
In fiction, one of the most potent grimoires is the Necronomicon, invented by horror author H.P. Lovecraft and allegedly written by the Mad Arab, Abdul Alhazred, as he detailed the blasphemous rites of the Old Ones. Lovecraft wanted to use elements of traditional folklore to create what he described as "new artificial myths." He was more ambivalent about the consequences of his myth-making. "I am opposed to serious hoaxes, since they really confuse and retard the serious student of folklore. I feel quite guilty every time I hear of someone's having spent valuable time looking up the Necronomicon at public libraries."
Lovecraft's work was built upon by other authors and also became the basis for several roleplaying games, most notably Call of Cthulhu and Pelgrane's Trail of Cthulhu. In these games the protagonists seek the secrets of the Mythos in order to save the world, often sacrificing their Sanity in search of tomes that will teach them the nature of their enemy.
Mythologically speaking, wizards derive their power from written knowledge and can therefore draw on that knowledge whenever they wish. Making a permanent record means that, theoretically, errors are avoided. Foggy memories of what the ancestors used to do when they wanted to make it rain shouldn't come into it. Where the mythological grimoire promises to teach its reader control over supernatural forces, their fictional counterparts offer writers a means of control over their narratives.
Lovecraft used the Necronomicon as a means of conveying information. In his short story The Hound, the protagonists realize what they're up against only because they've read of such things in the Mad Arab's text. "All too well did we trace the sinister lineaments described by the old Arab daemonologist; lineaments, he wrote, drawn from some obscure supernatural manifestation of the souls of those who vexed and gnawed at the dead." Here the book is doing double duty, allowing the author to give a description of the thing without going into excessive detail, and hinting at a long and awful backstory, without overwhelming the reader with that backstory.