As a former Sailor, it's not the pregnancy that's being court-martialed, (UCMJ'd, Office Hours or Captain's Mast), it's the adultery charge. Adultery happens to be illegal in most states as well, but I don't know of any civilian ever prosecuted for it. However, in a military unit, fraternization (senior to junior) or onboard (Navy ship) relationships are illegal. However, there's only three ways to PROVE it: caught in the act, legal statement by one of the participants or pregnancy.
A deployed, MARRIED servicemember who turns up pregnant when she wasn't pregnant when deployed is messing around, which is not good for unit morale and is against the UCMJ, as husbands and wives are never deployed together.
Single service females who turn up pregnant are sent home and need to arrange dependent care plans within 4 months of the child being born, as they are then eligible to being redeployed.
I think this is mostly a warning to keep hands off in a combat zone. If EITHER service member is married it's adultery. It's the law. And the law is being enforced to fight a war.
The fact that military members are held to this higher standard then the civilian population is part of the deal. In the civilian world, all a guy can do with a spouse who fools around is to divorce her and not pay child support for a baby that isn't his. As for women, you can tag another guy and have him pay for your kid while their wife (and your husband) can either throw the two of you out or deal with it.
God Bless those who go in harm's way.