Fantasy has the problem of being 'like the middle ages/renaissance, but with magic and dragons.' Magic and dragons mean that many of the traditional components of real medieval life* aren't necessarily the best fit for a given situation. However, if you deviate too far from the medieval setup, you're disrupting the escapist fantasy your audience was seeking in the first place.
*not just the arms and armor and castles stuff, although in a combat-heavy game those naturally get a lot of spotlight
Some settings will acknowledge this directly, and include some non-medieval components (such as magical gates networks or Continual Light streetlights or Eberron's genie powered trains or whatever the specifics were-- often gated behind a 'too expensive for the common folk' gate to explain why there are still caravans and lamp oil to buy and such). Likewise, most settings will over-emphasize the medieval tropes that best explain things like 'so why don't _____ absolutely dominate here?' -- such as the overemphasis on ballista as compared to catapults or other such weapons, since they make the most sense in a work with flying threats.
However, at the end of the day, fantasy worlds have castles and knights on horseback and weapons designed to fight other humans and common careers meant to evoke at least the renaissance fair-level understanding of what life was like bitd because people interested in fantasy signed up for that form of escapism.
Now, if you are trying to find an explanation for why that works and why it was done (keep your verisimilitude intact, as it were), I would say that it is because castle walls are 1) cheaper and easier to build in FR than real medieval settings, 2) good enough against enough kinds of threats that they are still worthwhile, and 3) threats which can obviate walls as a defense (aerial and incorporeal creatures) are rare enough and dangerous enough that you have another separate line of defense for them (perhaps you have normal guards on walls for the standard orc armies and owlbear infestations and when the dragon comes a calling you summon the town wizard and bard the archer with his quiver of dragon-slaying arrows).