I would expect most countries to allow exceptions to their usual immigration procedures in cases of emergency (there may even be some pieces of international legislation about this). Japan does, in Article 17 of its Immigration Control and Refugee Recognition Act:
In the case of disease or any other accident which urgently requires
the landing of a foreign national aboard a vessel or aircraft for the
purpose of medical treatment, an immigration inspector may grant
permission for emergency landing to the foreign national concerned
based on an application by the captain of the vessel or aircraft or
the carrier who operates the vessel or aircraft until the cause
thereof ceases to exist, subject to a medical examination by a
physician designated by the Minister of Health, Labour and Welfare or
the Minister of Justice.
(Note that in Japanese immigration parlance, landing means entering Japan by passing through immigration, etc.)
Credit:stackoverflow.com‘