All the best-loved authors, it seems, now leave a last book, to be published posthumously – Joan Aiken, Agatha Christie and (supposedly) Stieg Larsson, to mention just a few – and now, with The Shepherd’s Crown, it is the turn of Terry Pratchett, who died in March 2015. Works from beyond the grave give fans one final entertainment but they can also act as a covert last will and testament in which what an author really believes is made more explicit.

Of all his many heroes and heroines in the 40 novels of the Discworld series, the one Pratchett chooses for his envoi is Tiffany Aching, first encountered in The Wee Free Men. That novel, which drew on both A Midsummer Night’s Dream and the fairy paintings of the Victorian parricide Richard Dadd, is one of his most interesting: for in fantasy, those with magical powers are more than usually avatars for the author, and the struggles they undergo tend to grapple with the stuff of fiction itself. Pratchett, with his sardonic inventiveness, social satire, play on language, deep feeling for landscape and love of what is best in human nature, had less critical praise than he deserved. His heroes and heroines are not royalty in disguise, but thieves, con-men, shepherds, soldiers and midwives. In his championing of the ordinary, the sensible and the slightly silly he went against the grain – and never more so than in creating Tiffany Aching.

Tiffany, like her author, is patronised for all kinds of absurd reasons. She is young, and the daughter of a working-class family who joke after a hard day’s work that they are “Aching all over”. Other witches believe she can’t be a witch because she comes from the wrong kind of land, the Chalk, supposedly too weak to be magical. She has a sense of humour so dry that it passes most other characters by. As a witch, she becomes a kind of health visitor on a broomstick, helping with births, deaths, arthritis and the cutting of old men’s toenails. Yet she has defended her land and its people from its greatest enemies, and the most terrifying witch in Discworld has chosen her for an heir. When the novel begins, this witch, Granny Weatherwax, is dying. What follows is, in many ways, a valediction against mourning, and in its wit, courage and kindliness it often brings tears to the eyes.

Pratchett discovered he was suffering from “the embuggerance”, Alzheimer’s, in 2007. Before his death at 66 he wrote five last novels and planned many more. He spent much of his career thinking about how people die, Death being one of his best and most regular characters in the Discworld series. A skeleton with glowing blue eyes, capitalised conversation and a courteous, inexorable manner, Death comes for Granny Weatherwax when she has made her last, meticulous preparations. She has had a long, well lived life and it’s an inconvenience to leave it, she tells him, but it come to us all. Like a friend, he takes her so gently she doesn’t even notice.

Naturally, the departure of such a powerful person brings Discworld into danger, for outside the mortal lands are those of Fairyland and its cruel, amoral, greedy elves whose Queen is an old enemy of Tiffany’s. How can our heroine step into the shoes of Granny Weatherwax? Can she follow her own path as a witch, even if it means abjuring romantic happiness with her medical student boyfriend? Older witches, such as the nauseating Mrs Earwig, sneer at her inexperience. It’s when the Queen of the Elves, ousted from Fairyland in a political coup, turns up naked and close to death on Tiffany’s doorstep that the fun really begins.

Of course it is riotously funny, with the gloriously irrepressible Nac Mac Feegles having the best jokes and fights; as bright blue warriors otherwise known as the Wee Free Men they are shrunken but fearsome Scottish Nationalists; the Elves and their quarrels may well recall other politicians south of the border.

The real battle, however, is between selfishness and duty. Pratchett has rarely been so direct. It’s tempting to think that in this, his last book, he felt able to drop his customary teasing through footnotes and explain what empathy is. When Tiffany teaches the former Queen of the Elves about why it’s wrong to be spiteful and selfish, she is quite explicit. Kindness is worth practising because other people matter, because it makes you feel better, and because “what goes around comes around.”

Only in Discworld could the path of virtue be chosen simply by having it pointed out to you. Pratchett has always sent up Tiffany’s copy of The Goode Childe’s Booke of Faerie Tales and its conventional views on witches and fairies, but his stories also invite us to think about what constitutes good and evil, whether a character is inventing the postal service or serving in a battalion with a vampire. When Tiffany, in a rage at finding some elves kidnapping a baby, kills them in a moment of fury, it is not a triumph. She realises that to think of anyone as “just” anything is “the first step on a well-worn path that could lead to, oh, to poisoned apples, spinning wheels and a too-small stove… and to pain, and terror, and horror and the darkness”.

Those who love Pratchett find him, as AS Byatt has said, to be a lifelong source of pleasure and happiness, but this comes at the price of not showing us “the darkness”. There is a bullying father here, and spite and sudden death, but none of it disturbs. Other great fantasy authors from Tolkien to Robin Hobb leave us in no doubt that the torture, rape and murder in their worlds, described in chilling detail, are real and terrible, like the lust for power and sex that inspires them: but the filth of the city of Ankh-Morpork is down to dirt and poor plumbing. We are so used to the way George RR Martin or Joe Abercrombie or even Ursula le Guin show us fantasy worlds riven with cruelty, that perhaps the kindliness of Discworld is more subversive than it seems. It is, in essence, a humanist’s creation in which laughter, as Nabokov said, is the best pesticide, and humour as potent as swords. When, in The Shepherd’s Crown, something that looks quite like the Christian concept of the devil appears, he is the King of Fairyland, living in a barrow which “reeked of masculinity and unwashed clothing, of feet and sweat”. It is a very Pratchettian joke to bribe this bored, lascivious, violent and irresponsible supermale to depart from mortal affairs by offering him – a shed.

At its heart, this is a book about death, courage and humility. The shepherd’s crown of the title begins as a little soft creature which dies but leaves a sharp core in the Chalk that becomes a source of hidden protective magic. Tiffany discovers its power at the moment of crisis – not as a queen, or a witch, but as a true shepherd, guarding and leading the people she puts before herself. It’s only because she is selfless that she can defeat evil: a story that many have heard before, but which bears repeating, albeit in hope rather than expectation.

This is not a perfect example of Pratchett’s genius, but it is a moving one. Pratchett was too generous, I think, to leave his readers without consolation. Only those who read the Feegle Glossary at the back will notice that the very last word in this last novel is “despair”.

The Shepherd’s Crown is published by Doubleday (£20). Click here to order it for £16