Many ancient cultures of the Copper, Bronze and Iron Ages from Egypt (during the First Dynasty 3100 BCE to 2900 BCE) to China, at least, and perhaps even in Oceania and the New World (e.g. in the Mississipian culture), has a practice of burying deceased leaders with attendants (retainer sacrifice) and/or family members (particularly widows) who were either killed for the purpose or died after being buried alive in a particularized form of human sacrifice.
(The Chinese Terracotta Army ca. 210 BCE, appears to be a reform of this traditional practices that replaced actually afterlife attendants with symbolic representations of them. And, Egyptian burial practices underwent a similar reform after the First Dynasty with statutes of servants replacing acatual servants.)
A new series by Kate Elliot called Court of Fives has a speculative fiction world that has such a practice, fittingly as she has substantial training and experience in anthropology and history.
The broad geographic and temporal range of the practice suggests that this practice is something that a wide variety of early organized religions. Broad outlines of early religious practice seem to be near universal, and as Robert Wright notes in his book, The Evolution of God, all available information suggests that these societies were pervasively religious one.
Essentially, then, it seems that a very common path of religious development, whether or not it is actually truly universal, is for religion to imagine an afterlife, and further, to imagine that important figures who were served by many in life should be likewise served by many in the afterlife by having servants who die with them.
It isn't at all clear to what extent the people whose lives are sacrificed in the process, or those who carry out the rituals, are sincere in believing that an afterlife with companions will follow, and to what extent the sacrifices are forced into it not believing that an afterlife will follow and those who choose the sacrifices are using the practice for anything other than expedient political murder, regardless of what the larger society believes.
Likewise, the practical impact of this practice on societies and any evolutionary benefits conferred by it are unclear, although given that so many societies have practiced it, surely there must be some.
One possibility is that most ancient societies were close to a Malthusian limit which periodic culling of people who needed its resources eased somewhat, often at a time when expert leadership was replaced by neophyte leadership that might make errors reducing the economic resources available to the community, in an antiseptic manner.
Another possibility is that what really mattered was who was chosen to join the leader in death, and that choosing people loyal to that leader removed from society people who might not share that loyalty towards the successor leader, thereby reducing dissent and assuring the stability of the new regime.
A third possibility is that often the old leader's trusted aides were chosen when he took office and by his death were themselves often feeble of mind and body, and that culling these aged aides opened the door to a new generation of younger, more capable leadership to come to the fore. This would mitigate the initial concern that removing top talent in the leader's court from society would be a catastrophic loss of social capital that would hurt the society (unless those who accompanied the leader into death were mere stand ins for the aides).
At any rate, it is an interesting and widespread practice that deserves more consideration.
With regard to Kate Elliot's book that inspired the thought, Circle of Fives, whatever merits or lack thereof can be attributed to it as a literary work (the reviews range from extremely positive to extremely negative), really does deserve to be admired for its world building. It draws on Mesopotamian (i.e. Sumerian) and Hurrian/Hittite sounding names, and has been compared by reviews to ancient Egyptian, ancient Greek, ancient Roman and ancient Mayan cultures respectively, with a couple of smatterings of phenomena that may be magic, may be technology, or may be indeterminate (like metal war machines called "Spiders" that sound suspiciously like Steampunk technology even though the general milieu has more in common with far more ancient cultures).
It is artful, of course, because while it draws on general concepts present in many of these societies, is not just an remaking of any one of them and is instead an artful synthesis of historical cultures of the appropriate technology and civilization level that doesn't precisely replicate any one of them (although I would lean towards Sumerian and Egyptian leanings).
Another thing that her world does a good job of exploring is what a society in which a superstrate conquering population rules over a substrate conquered population which speaks a different language looks like, from the wonderfully liminal position of a protagonist who is the child of a male soldier who belongs to the superstrate culture and a local woman (a very common situation in ancient history and the Holocene prehistoric past in almost every region on Earth).