What kind of novel is Neil Gaiman's American Gods? In large branches of Waterstone's there is a special room, segregated from the rest of contemporary fiction and labelled "Sci-Fi, Fantasy, Horror". It is as if these genres were intimately connected ("Crime", the other dominant genre, is off in a room elsewhere). Gaiman's novel has traits of the first and the last of these: the narrative seems headed to some future calamity, and contains an elaborate sub-plot of small-town horror. But essentially it is a knowing and erudite example of the genre that we have come to call "fantasy". As a type of fiction for adults, the genre was invented in the 19th century: Dickens's A Christmas Carol has the logic of fantasy, but George MacDonald's Phantastes (1858) is probably the first example of a full-length fantasy novel, revealing to its Victorian protagonist a dangerous parallel world of "Faerie" adventures.

Ever since, one type of fantasy has connected the familiar with the invisible. In American Gods, you know you are in a fantasy novel when the protagonist, Shadow, finds that the stranger sitting next to him on a plane already knows his name and his history. From this meeting onwards, he is part of a plot that cheats probability. He is befriended by eccentric characters who appear and disappear at will, and have tasks for him that are never fully explained. Meanwhile politely sinister men in black suits stalk him as he travels across the States. His wife Laura has been killed in a car accident, but she comes back from the dead not only to speak to him (this might happen in any novel with a grieving protagonist) but also to intervene in the plot.

Sent to Kansas to collect the body of his murdered employer, the one-eyed confidence trickster Mr Wednesday, Gaiman's hero is told that he must wait until midnight to perform his duty.

"These things must be done according to the rules," said Czernobog. "All things have rules."

"Yeah,' said Shadow. "But nobody tells me what they are."

This is the essence of fantasy narrative. The rules that govern nature can be flouted – spirits and deities exist, the dead will come back to life, dreams can come true – but a new set of rules takes their place. The challenge for the hero, and for the reader, is to discover what they are.

One rule is that a character's name is likely to reveal his or her true identity. Readers au fait with Norse mythology will have divined something about the life of Mr Wednesday beyond this novel. It is a satisfaction of much fantasy that it draws on legends that already exist. Readers of American Gods who do not know the legends that Gaiman has used come to recognise them because the hero does. Also, interleaved between the numbered chapters of Shadow's adventures, are unnumbered chapters headed "Coming to America", in which we get yarns of how travellers to America might have brought their own peculiar spirits and legends to this new land. The near contemporary America is full of gods, large and small, brought and then abandoned by its people.

When characters from mythology turn up in the novel, you know that it is all part of some design. In a motel car park somewhere in the midwest, Shadow bumps into the man with whom he once shared a prison cell. Hundreds of pages earlier, Gaiman's protagonist might have thought it natural that a jailed felon should be called Low Key Lyesmith. Now the coincidence of this meeting jolts him into an éclaircissement:

"Loki," he said. "Loki Lie-Smith."

"You're slow," said Loki, "but you get there in the end."

Like all self-respecting pagan gods, Gaiman's deities enjoy the human befuddlement that they cause. The joky tone of this exchange is characteristic of the novel, which delights in the incongruity of relocating ancient myths and deities in the small towns of America, observed as closely as only a non-American writer could manage. (One running joke is the frightening fast-food that all these deities are obliged to consume by dint of being reincarnated in the US in 21st-century America.) Gaiman's gods speak like wise-cracking modern-day Americans.

The reader takes on trust this sense of design, and woe betide the fantasy writer who betrays it. Though anything can happen, nothing happens by chance. Though American Gods begins with news of a terrible accident, the experienced reader of fantasy narrative will know that, in such a novel, each accident has its deeply planned causes, and the character who finds himself the hero is not the hero by chance.

John Mullan is professor of English at University College London. He will be talking to Neil Gaiman at the Edinburgh international book festival at 3pm on 21 August.