Pratchett was described as an "international publishing phenomenon" by the prize organisers.
Judges said his winning book, The Amazing Maurice and His Educated Rodents, was an "outstanding work of literary excellence".
Pratchett, who has become a favourite among adults and children with his Discworld series, selling more than 27 million books worldwide and recently being named as the UK's best-selling author of the last six years.
"I'm totally delighted and genuinely shocked," the author said, adding that he would have bet £1,000 that he would not win.
"I'm especially pleased because Maurice isn't just fantasy but funny fantasy, too. It's nice to see humour taken seriously," he said.
The Carnegie Medal is awarded to the author of the best book of the year for children and young people.
'Subversive'
Pratchett had been nominated for the medal twice before, and was up against seven other writers on the shortlist.
Recent Carnegie Medal winners
2000 - Beverley Naidoo for The Other Side of Truthe
1999 - Aidan Chambers for Postcards From No Man's Land
1998 - David Almond for Skellig
1997 - Tim Bowler for River Boy
1996 - Melvin Burgess for Junk
1995 - Philip Pullman for His Dark Materials: Book 1 Northern Lights
Karen Usher, chair of this year's judging panel, said this year's decision had been unanimous.
"This is an outstanding work of literary excellence - a brilliant twist on the tale of the Pied Piper that is funny and irreverent, but also dark and subversive," she said.
"Terry Pratchett uses his trademark wit and humour to question our society's attitudes and behaviour in a way that is totally accessible for children of 10 years and over."
The winning book sees a clever cat, Maurice, lead a group of intelligent rats to invade a town in a money-making scam, before a boy called Keith is paid to lead them away with his pipe.
Plastic Gandalf
Pratchett said he hoped his work offered a wider view than JK Rowling's Harry Potter novels or The Lord of The Rings series.
"Fantasy isn't just about wizards and silly wands," he said.
"It's about seeing the world from new directions."
But he conceded that fantasy writers were benefiting from their success.
"When you buy a plastic Gandalf with kung-fu grip and rocker launcher, you know fantasy has broken through," he added.
The judging panel awarded the highly commended title to Geraldine McCaughrean's Stop the Train, about a group of settlers in the United States in the 1890s.
US writer Sharon Creech's Love that Dog was commended. The shortlist had been compiled by librarians across the UK.
The Carnegie Medal, first awarded in 1936, has previously been given to Arthur Ransome, CS Lewis, Anne Fine and Philip Pullman.
Its sister award, the CILIP Kate Greenaway Medal for illustration, was announced at the same time and went to The Observer cartoonist Chris Riddell.
His well-researched drawings illustrated Pirate Diary, an account of a boy's travels on the high seas.
It was the first "information book" to win for 27 years, judges said, praising Riddell's "incredible variety of style and gift for capturing character".