It is permissible if there is an interest to be served by that and it will not cause a greater evil, and that is not regarded as mutilation. What our companions [i.e., the Hanbalis] said is well known. They said that if a pregnant woman dies and there is a living child in her womb, it is haraam to open her abdomen, but some women (midwives) should try manually to bring out the foetus who it is hoped might live. If that is not possible, then she should not be buried until the one who is in her womb had died too. If (the child) has been partially born and is alive, then a cut may be made in order to birth the rest of him. This is the view of the fuqaha’, based on the view that this is mutilation by cutting, and the basic principle is that mutilating the deceased is haraam, unless it serves a certain greater purpose, i.e., if the child has been partially born and is alive, then a cut may be made in order to birth the rest of him, because that is in the interests of the infant, and because not cutting in this case will lead to the negative consequence of the child dying, and the living should be given greater care than the dead. But in modern times, when the art of surgery has become more refined, cutting the abdomen or any other part of the body is not regarded as mutilation; they do it to the living with their consent and agreement for various kinds of treatment, and it is most likely that if the fuqaha’ had seen that, they would have ruled that it is permissible to cut the abdomen of the pregnant woman in order to extract her living foetus, especially if the pregnancy is full term and it is known or thought most likely that the foetus is alive.
Another thing which points to it being permissible to cut the abdomen and extract the living foetus is the fact that if there is a conflict between pros and cons, then the greater good should be given precedence and the lesser evil may be committed. Sparing the abdomen from any cutting is good, but saving the child and keeping him alive is a greater good. Cutting the abdomen is bad, but leaving the living child to suffocate in her womb until he dies is a greater evil. So cutting becomes the lesser of two evils. Moreover, we say that in such cases, cutting is not regarded as mutilation or evil, so there is nothing to rule out extracting the foetus at all. And Allaah knows best. End quote.
Shaykh ‘Abd al-Rahmaan al-Sa’di (may Allaah have mercy on him)