Here's what happened:
* Gellert Grindelwald stole the Elder Wand from Gregorovitch.
* Dumbledore defeated Grindelwald in a duel, but did not take the Elder Wand from him.
* Grindelwald was imprisoned and the Elder Wand remained in his possession.
* Dumbledore later visited Gregorovitch and, through clever questions and observation, figured out that Grindelwald was the wand's owner.
* Dumbledore used this information to figure out where Grindelwald was imprisoned and eventually disarmed him, thus becoming the wand's new master.
It's important to note that the Elder Wand can only change masters through a duel where the current owner is disarmed.