Three intertwined magical mechanisms saved Harry in Philosopher's Stone (1981) and Deathly Hallows (1998):
Lily chose to die rather than step aside. This voluntary, loving sacrifice invoked old magic that:
When the curse rebounded (1981), a fragment of Voldemort's shattered soul latched onto the only living thing: Harry. This made Harry an accidental Horcrux. In Deathly Hallows, Voldemort's own curse (cast with Elder Wand) destroyed the soul fragment inside Harry, not Harry's soul—because Harry was the true master of the Elder Wand (won from Draco, who disarmed Dumbledore).
The Elder Wand refused to kill its master. In the Forest (1998), Harry chose to die (sacrifice for friends), extending Lily-type protection to all Hogwarts defenders. When Voldemort fired the final curse, the Wand turned it against him.
Love (Lily) → Rebound → Soul Fragment → Wand Loyalty → Survival.