Ancient religions throughout the earth have been practicing human and animal sacrifice for centuries before the advent of Judaism and christianity and its not hard to see how the offering of sacrifices arose.
Ancient people attributed agency to deadly events like droughts, storms, volcanic eruptions, etc. They assumed that a god or gods were behind them and brought them to take human lives. So if the gods bring horrible disasters to get human lives how do you stop the gods from bringing the disasters? Simple. You find a way to give the gods what they want - human lives - so that they would have no need to bring the disaster to get them. So you offer the gods sacrifices. True, lives are still being taken. But the difference with sacrifices is that the community gets to feel like they're at least in control of the situation. They can determine who dies and when they die instead of having to be in the powerless situation of a god unexpectedly bringing a disaster to kill god knows how many, who and in what manner.
But then another problem arises - the amount of people killed in natural disasters are invariably more than the amount of people that will be sacrificed to avert them. Why would a god accept the death of one person served up on a platter when he can claim a hundred lives in a flood? This idea likely troubled the ancients. So how did they resolve it? They don't just offer up anyone. They offer up the best among them- the most beautiful, the most strong, the most virtuous - the virgin - the one or few who are the most perfect in the community. Surely the gods would prefer to have a few guaranteed high quality lives served up to them on a platter rather than working to take lives in a natural disaster where persons of all sorts die with no guarantee that they'll get a life of good quality. And thus the notion of having a suitable sacrifice of high quality - an unblemished lamb, an innocent, chaste virgin, etc.
Sacrifices also work for the belief that the gods bring disasters to punish the people for their sins. Instead of the whole community being punished for their sins by the gods, why not have one individual bear the sins of the whole community and be punished on behalf of the whole community? But if this person is himself a notorious sinner then it would stand to reason that he deserves punishment for his own sins. So perhaps when the gods are finished with punishing him for his own sins there wouldn't be any of him left to be punished on behalf of the rest of the community and the gods will still punish the community for its sins. So to resolve this issue, the one who is to be sacrificed to the gods for the sins of the community, has to be one who is innocent enough that his own sins are not so numerous that he has no room left in himself to also carry the sins of the community.
Killing the sacrificial victim in an especially slow, painful and brutal manner could also have been thought up as a way of justifying the death of one man or woman for a whole community. Let the gods see the lone victim experiencing the brutality equivalent to experiencing a thousand deaths and maybe the gods will agree that his death is more than sufficient to compensate for the hundreds of deaths that the gods would have otherwise taken by some natural calamity.
The whole concept of the ransom sacrifice integral to christianity and the animal sacrifices of Judaism go way, way back in time much earlier and more primitive pagan cultures who devised the concept of sacrifices through superstitious beliefs.