Underground fortifications are very defensible, far more so than any above-ground fortification. However, the main reason that they are rare is the time and expense in excavating them, amongst other reasons.
Assuming a medieval environment with flying mounts in common military use, underground fortifications make a lot of sense, and their added defensive value compensates for the additional expense and the other problems they have.
Most of us should be familiar with medieval surface castles, and there are plenty of internet resources describing them for those that don't, so I won't describe them here for the most part.
The first difference between underground and above-ground fortifications is their attackable area. A surface fort has a large attackable area, effectively the whole perimeter in most cases (including their upper aspect if there are flying mounts), whereas an underground fort has a very small attackable area, effectively only any openings in the ground.
Like a surface fort, an underground fort would have traditional architecture facilitating an active defense such as arrow slits, murder holes, portcullises and gates defending its openings to the surface. Gates and portcullises, rather than being wooden and hinged, could be stone and round, so that they would roll into place sideways. Proper design would prevent their simply being pushed inwards, and a gate could be made immensely thick, yet easy to roll from inside with the use of a lever, making battering a suicidal tactic. A gate can be locked by the simple expedient of placing a wedge behind it. Arrow slits would have the additional advantage that there should be no question of them being open to the sky, making plunging fire as a means of attacking an unmachiolated wall (a machiolated wall has wooden hoardings that make this tactic less effective) of a surface fort ineffective.
Given the lower attackable area of an underground fort, it could easily occupy a very large volume of a hilltop, and yet require a wartime garrison smaller than that required for a similar sized surface fort in peacetime.
Now, we get to problems. Underground forts have the same non-combat problems as any fort, i.e. supply of food and water, with the additional complication of requiring a supply of air. While this can be achieved by simply cutting narrow tunnels to the surface, these ventilation ducts become potential avenues of attack in their own right. Since the fort is underground (that is its whole point), it is difficult - and should ultimately not be attempted - to deny an enemy access to the whole surface above the fort.
In some environments, such as dense woods, it may be possible to have concealed ventilation, and I would expect that many underground forts would have such. However, since an attacker could easily clear any such concealment by the expedient of arson, I would expect that there would be one, or more likely two, fortified ventilation duct headers. These would be basically a fortified chamber at the head of the open ventilation duct, accessible by a separate personnel tunnel. They would likely be designed to prevent the defenders of the vent header from egressing onto the surface for the simple reason that if the defenders cannot get out, attackers would have a far harder time getting in. The effect of a defended vent header would be that they could deny an enemy the ability to smoke the defenders out for as long as the header remains manned with active defenders.
Since the issue of ventilation would be so important in an underground fortification, it would not surprise me if a Sodium hydroxide carbon dioxide scrubber was invented in the medieval period. It may not be known what such an item does to maintain air quality, just that it does, and Sodium hydroxide can have a secondary use as a particularly nasty substance to drop from a murder hole onto an attacking enemy. It also requires hydrated lime, which is easily made/regenerated by cooking limestone in a kiln to drive off the CO2, and hydrating it with water. A Carbon dioxide scrubber would greatly increase the amount of time the defenders of an underground fort could survive an attempt to smoke them out, by the simple expedient of closing the vents and activating the scrubber. This would mean that the limiting factor to survival would not be CO2 buildup, but oxygen supply, which typically lasts longer than the time for lethal levels of CO2 to build up in a confined space. With only a small number of defenders and a large internal volume, survival could be extended potentially to weeks in a completely sealed situation. Smoke scrubbers in the form of water-filled tubs through which outside air could be bubbled could allow reduced ventilation in a smoke-attack situation for quite some time, and water would also absorb a number of other potentially toxic substances that could be used in an attack. Water would naturally be on hand for purposes of drinking, even if there was a well (which is likely).
Another problem that an underground fort has is external visibility. In a surface fort, defense is achieved through high structures, which provide the bonus of being a good vantage point over the surrounding terrain. An underground fort would lack this natural advantage to some degree (depending on where it was built), but in situations where there was limited height advantage over the surrounding terrain, a single tower could be built, rather like a minaret, which would be relatively easily defended from ground and airborne attackers, and could be abandoned easily if attacked (by which point the defenders should be aware that they are under attack).
Since the arguments for location of an underground fort are similar to those for a surface fort (a high geographical feature), flooding is unlikely to be an issue, however drainage of waste products would be an issue, requiring somewhat better than historical medieval plumbing, but not impossible to achieve, since the Romans who predated the medieval period had already accomplished the necessary engineering.
Derinkuyu, Kaymaklı and Özkonak underground cities in central Turkey, built around the 8th-7th century BC, could be considered early precursors to medieval structures such as I have described above.