I suggest avoiding the word "discriminate"—its emotional overtones are too strong. Here we're merely differentiating between multiple candidates, and doing our best to be fair. Enshrined in law is the notion that it's unfair not to hire someone merely because she's planning to take maternity leave shortly after starting. I admit that calling this "unfair" confounds my intuition—forcing a company to subsidize a new mother strikes me as unfair, and neglecting to disclose a pregnancy borders on fraud—but perhaps I can trigger a sense of unfairness even without reference to the company's well-being.
Suppose Alice and Barbara both apply for a job for which they are equally qualified. Alice is newly pregnant, but (per the law) the prospective employer isn't allowed to inquire about this. If the company hires Alice, the company suffers—and so does Barbara. It's impossible to afford pregnant women special treatment without harming women who aren't (and perhaps don't plan to become) pregnant.
Life is full of choice and chance. When trying to legislate away the consequences, expect bad side-effects.
What if Alice didn't know she was pregnant -- or wasn't even pregnant -- at the time of the interview? The process of hiring can drag on for weeks and months for many industries and companies.
The employer made what you've described as a coin flip choice and have to stand by that decision. Life is never "fair" in the way you're defining fairness in this case.
It's the same basic situation, just not as severe. Alice undoubtedly has some inkling that she may become pregnant, even if she thinks it unlikely. If Barbara isn't pregnant or planning to become pregnant, and has taken steps to prevent pregnancy, she is still a better hire than Alice, all things being equal. In a competitive market for labor under complete freedom of contract, a company could ask both Alice and Barbara about their plans to become a mother. The employment contract could also include clauses contingent on pregnancy, such as indemnifying the company against the lost productivity caused by unexpected maternity leave. The details would be determined by the relative bargaining power of the company and prospective employee. Those offended by such practices could simply decline to apply to such companies.
In short, under freedom of contract, company policies with respect to pregnancy would depend on free negotiations between buyers (employers) and sellers (applicants). The solutions would likely vary widely, each tailored to the needs of its particular industry. In effect, the status quo consists of a mandatory one-size-fits-all (or perhaps one-size-fits-none) solution that has many more negative side-effects that its proponents are prepared to admit. (Exercise: Show that compulsory compensation for maternity leave necessarily increases unemployment and lowers wages.) The idea that current policies necessarily benefit women is naïve at best, evil at worst.
Incidentally, there's nothing necessarily wrong with governments subsidizing procreation. But deciding whether and how to do so requires judgment and wisdom—qualities not particularly evident in those currently in charge.
As someone who became mother due to the lack of active prevention of pregnancy (call it a pleasant surprise), I am of course of a different opinion than yours.
There is nothing stopping either Alice or Barbara from lying about their procreation plans. Outside of abstinence, which cannot be proved or disproved by anyone other than the female in question (and it is not even something she has 100% control on -- unwanted sex do happen in this world), there is no foolproof birth control method. As such, and without trampling on employees' privacy, there's no point for an employer to inquiring about procreation plan.
I don't believe the clauses contingent on pregnancy is appropriate on employment contracts neither. If maternity leave is such a big loss on productivity that a company needs compensation for, one can argue either the market should already have a product for it (e.g. some form of "maternity insurance" provided to employers), or the government should subsidize the employer.
In Canada, where @raganwald and I currently reside, employers are not legally required to provide compensation for persons on maternity or parental leave. (Maternity leave is for mothers giving birth only; parental leave can be split between parents of either birth or adoption.) Instead, the government provides partial compensation. It's up to the employer to decide whether to provide additional compensation on top of government benefits. I don't know where you are located nor your local laws regarding maternity leave. In your opinion, would a policy similar to Canada's be suffice, or do you still want more protection for the employers?
I suppose you are opposed to Arizona's new birth control pill bill HB2625 then? [tongue in cheek]
Suppose Alice and Barbara both apply for a job for which they are equally qualified. Alice is newly pregnant, but (per the law) the prospective employer isn't allowed to inquire about this. If the company hires Alice, the company suffers—and so does Barbara. It's impossible to afford pregnant women special treatment without harming women who aren't (and perhaps don't plan to become) pregnant.
Life is full of choice and chance. When trying to legislate away the consequences, expect bad side-effects.