Here's a breakdown:
1. Grindelwald stole the Elder Wand from its previous owner, Gregorovitch, a wandmaker. This was done through a duel, which means the Elder Wand now considered Grindelwald its master.
2. Dumbledore and Grindelwald, who were once friends, eventually had a duel over their conflicting views on how to achieve a better world.
3. Dumbledore won this duel, and the Elder Wand, being a powerful object that acknowledges its master's strength, transferred its allegiance from Grindelwald to Dumbledore.
It is crucial to remember that the Elder Wand's power doesn't lie solely in its magical potential but also in the ownership and allegiance of its master. It is a powerful tool that can only be truly wielded by a worthy and powerful wizard.