Libertarian themes are common in science fiction. Several of Heinlein’s works have clearly libertarian ideas, and several other authors, including F. Paul Wilson, L. Neil Smith, and J. Neil Schulman (there must be a reason for the first initial-middle name pattern), have written hard-core libertarian SF. Ayn Rand’s Anthem is science fiction, and Atlas Shrugged has important SF elements.
Science fiction is about exploring alternative possibilities, and the analytic approach that’s common in SF appeals to many libertarians. There is, of course, also a lot of science fiction with clearly non-libertarian ideas, promoting socialism, scientist-kings, benevolent alien overlords, and supposedly good galactic elites that hold arbitrary powers of life and death. A genre that deals in speculation will go in all directions.
In fantasy literature, though, I can’t think of any important work that I’d call libertarian. There’s a difference between works that are specifically libertarian and ones which might be called libertarian-friendly. There’s no lack of fantasy works in which tyrants are overthrown or would-be tyrants are frustrated, but those villains are so evil that no one would support them. You don’t have to be against income taxes and for legalizing cocaine in order to hate Sauron, Lord Voldemort, or the White Witch.
Some of these works have sections with special libertarian appeal. Tolkien’s Shire has almost no government and gets along very well. Terry Pratchett’s Small Gods presents an authoritarian religious state as the villains, and even its god learns to grant people more freedom. They’re far from explicitly libertarian, though. Some people have tried to present J. K. Rowling’s Wizarding World as libertarian, but the wizards have enslaved the house-elves and set up a literally soul-sucking prison.
Fantasy literature deals in magic, and it’s sympathetic to the idea that ideas can be solved literally by waving a wand. This has obvious appeal to progressives and socialists, who like to think that a sufficiently powerful government can make everyone well off in spite of the laws of economics. Libertarian ideas are built on the assumption that wealth has to be created and earned by thought and effort. Magical worlds are built on the idea that it can be created by inherent power, in effect by wishing. What you were born as often matters more than what you have made of yourself. Aragorn deserves to be king because of his ancestry. Muggles can’t levitate a peanut, no matter how much they study. Good and evil tend to be represented as cosmic forces rather than individual choices, and it’s necessary to follow the born leader in order to hold back the Forces of Darkness.
Obviously I haven’t read everything, and in fan fiction just about every possibility has been tried, so I’m sure there is libertarian fantasy out there. There are opportunities for trope-smashing stories or pushing the idea of the Promethean rebel. My filk song “De-liver Us from Evil” casts Zeus as a patent troll. Perhaps someone could do (or has done) a story of opening free trade between dwarves and elves?