In my experience as a Hindu, usually Karma is brought in to explain the suffering of innocents. Of course, this is actually a non-explanation because by saying that the suffering of an innocent is a result of their own past actions, we are implying that the said person is not actually innocent.
This always used to make me wonder: Is saying that someone deserves what happened to them count as good action? Will we suffer for saying so? One day, when we suffer for no mistake of ours, will someone else say that we deserved it? Will we say it to ourselves?
These explanations can be offered, but they do not appear to have anything to do with logic. They build on the idea that justice exists. And there is absolutely no evidence, I think, that justice exists outside of human society. Within human society, justice is a construct that has been put in place to maintain order. I have written about this previously in my answer to Why do people believe in an afterlife when there is and has never been any evidence? Who created the idea?
But anyway, I think the function of religion is to comfort people by offering them some kind of explanation for the things that happen around them. The alternative — saying that we do not know — is scary.
On the whole, members of every civilisation and followers of every religion have had to contend with the fact that bad things happen and suffering is something we all go through. Indians came up with the doctrine of Karma to explain it away just as Christians came up with the idea of Original Sin. Neither makes sense, but believing in them keeps our greatest illusion alive — that our existence matters to the universe.