Yes, it's clear why it's legal and necessary to some extent. In a for-profit system, a doctor's office or hospital, every procedure or test the doctor can order (and have the patient pay for) will generate profit. Doctors have an incentive to order as many tests as possible. I assume that most doctors are somewhat honorable and won't abuse this too much, but they'll probably still err on the side of ordering as many tests as possible not necessarily because of profits, but because more tests gives them more information.
Meanwhile, in a for-profit system, an insurance company will generate the most profit by agreeing to as few tests and procedures as possible. So, they will have an adversarial relationship with doctors and will try to arrange as few tests and procedures as possible. My guess is that the average insurance company is less ethical than the average doctor, so they're probably more likely to refuse to allow tests that are actually medically necessary.
In a sane system, there would be a neutral referee, the government, who would resolve disputes and severely punish any actor in the system that was behaving badly. But, AFAIK that only rarely happens in the US, where the idea is that the "invisible hand of the free market" will magically make it all work.