I believe that you're referring to the Darwath Trilogy by Barbara Hambly. Sounds most like the final book The Armies of Daylight (1983). It really could be any of the books in the trilogy, however, since "The Keep" where the people take refuge is a warren of caves and is a feature of all the books. The two leading characters are Rudy, who turns out to me a mage, and Gil, who turns out to be an excellent warrior. Both are young adults (college age) from California.
The reviews below, from Amazon, are not really the best. They are presented as is, however.
In short, the two young-adult characters become involved in the battle in another world. The kingdom is in a mountain-sheltered location, which turns out the be a bad thing. The kingdom, that survives, takes refuge in "The Keep," which is basically a fortified, and renovated, cave system in the mountains which is far enough away to involve a serious trek to reach that safe harbor. The creatures, "Dark Ones" only operate in darkness - at night or in caves - and destroy humans who encounter them.
In the first book the Dark Ones are expanding in the Darwath realm, and the source and cause are unknown, though it is learned that the Dark Ones can also cross the Void to California, land of our heroes, and that they are not native to Darwath is possible. The second book expands more on the Dark Ones, and the collapse of the realm's civilization under the attack. It is in the third book that we learn how, and why, the Dark Ones have come to Darwath, and why they might also be a threat to the world the heroes came from. Finding a new home for the Dark Ones, with its ethical implications, is dealt with in the third book as well.
To further match with your description, it is in the third book where the "battle" is taken to the nests of the Dark Ones, which involves a series of caves, tunnels and caverns throughout the realm. IIRC, this includes the wastelands outside the normal bounds of the kingdom. Some of the information in the question isn't known in the first book, or even in the second, so I'm thinking that either you read the series, or only the closing book.
It is not a childrens' book, it is however a young-adult book, and is often kept in the YA collection in libraries large enough to have such. Hambly's works often trend into dreary realms and dark thouhts, and are probably best used with caution. Nevertheless, I happen to like her writing, even when I don't like the way the story turns.
The Time of the Dark
Spawn of Darkness Night after night, Gil found herself dreaming and impossible city where alien horrors swarmed from underground lairs of darkness to destroy mankind and all the works of men. But when the wizard Ingold Inglorion crossed the Void to ask sanctuary for the last Prince of Dar, her learned her dreams had been true visions of a strange reality. On Ingold's world the monstrous Dark had been mere legends for 3,000 years. But now, for unknown reasons, they were ravening savagely across the land, and there was no escape from their foul powers and insatiable hungers. Attempting to help Ingold, Gil and Rudy, a young drifter, were drawn into the nightmare world of the Dark. Then, before they could realize their fate, the Dark stuck.
Wall of Air
Three thousand years before, the monstrous Dark had sprung from loathsome underground lairs to destroy most of humanity. Now they were again ravening and ruining in their blood-hunger. Only a few thousand people had managed to find refuge in the ancient fortress Keep of Renweth.
There, even the magic of the wizard Ingold Inglorion could offer them little hope against the Dark.
To defeat the savage horror, they must gain help from the Hidden City of Quo, to which all other wizards had been summoned. But Ingold could not pierce the walls of illusion that separated Quo from the world.
With his student, Rudy Solis, the old wizard set out to cross two thousand miles of dangerous desert to the City of Wizards.
What he might find there he could not know -- and dared not guess!
Armies of Daylight
After a year of magical combat, the world is in ruins, and the few surviving wizards, including two stranded Californians, must take to the offensive Since the Dark Ones returned, the world has been laid to waste. The land's wizards have been slaughtered, its cities destroyed, and its people scattered in terror, and few have witnessed more of the destruction than Rudy and Gil-two ordinary Californians who found their way across the Void, and took up arms in defense of a strange and magical world. She learned the ways of war, while he found within himself the powers of a great wizard. Both of them will need all their strength to survive this final challenge. Ingold, the master wizard, has devised a spell to hide the user from the deathly stare of the Dark, and he intends to use it to strike at their very heart. Finally, Rudy, Gil, and the rest of mankind's survivors will take the offensive, bringing an end to this terrible war, for better or for worse.