I think about this often, so let me try to play the devil’s advocate: (1) why are we (the society) allowed (even encouraged) to discriminate for intelligence at almost every point in our lives (school, job, marriage)? (2) sure, companies are forbidden from discriminating because of certain characteristics, let’s take pregnancy; but why should companies be expected to shoulder that burden all by themselves? It’s more than reasonable for the society to cover the costs, as a sort of insurance policy - it probably doesn’t matter that much for big companies that can just statistically assume that a certain percentage of employees will be sick / pregnant / etc. but for a small company 1 missing employee is a big burden (even if the government covers parental leave salary)
edit: could also include "assholeness" in (1), could even blame it on neurodiversity
If that is the case, obviously we discriminate by it, because we try to choose people who are better at X when trying to find people for X. Tether there is a single underlying characteristic that makes you better at X (a pre-determined IQ) that is discriminated for is perhaps harder to know. I certainly don't think that school for example advances students based purely on intelligence - discipline, even obedience, like-ability, sometimes teamwork, are all at least as important to what makes most good students successful.
Related to 2,my answer is very simple: companies don't take the full burden, at least in most countries. In general, there is a shared burden - the employee has reduced pay, the state is paying for some or most of that, and the company is forced to keep the position open for the employee when they decide to return. In my country at least, they are also free to hire a temp to fill that position for the 1 or 2 years of maternal leave.
In our society we have chosen a different solution, pretty much all over the world: your ability to have food and shelter is conditioned on having a job (or being born rich), but in exchange, the state tries its best to guarantee you a job. We don't generally allow the state to operate companies, so the only way for the state to guarantee jobs is to incentivize and even force companies to employ people without discrimination, to at least some extent.
Now, my personal belief is that the justification should come the other way around - why do we allow society to judge your right to a basic livelihood by any criteria at all? But I'm guessing that would not be a compelling argument, and I can't claim to have a working picture of how such a society could look like.
However, in practice, and especially for large companies, optimizing only for efficiency is simply not acceptable behavior. Since the ability to find a place to work is so important for any individual in our society to have a chance at a decent life, companies' social responsibilities must have them potentially sacrifice some efficiency to prevent discrimination of large groups.
Note: I am not in any way claiming that disadvantaged groups are less efficient at work, there is absolutely not evidence of that. I am claiming that sometimes, to accept disadvantaged groups in a workplace may create some discomfort to the people who were already in that workplace, since statistically they were likely to hold biases against that discriminated group.