How did Harry come to master the Elder Wand?
Harry takes Draco Malfoy's hawthorn wand, not the Elder Wand. How, then, does he become the master of the Elder Wand, which he never took from its true owner (Draco), or touched? Does a wizard lose the allegiance of all his wands if even one of them is taken from him? Pretty risky innit?
Since Draco took the Elder Wand from Dumbledore's hand (and thus, 'defeated' him, via disarming), the allegiance of the Elder Wand passed to him (even though Draco didn't use it).
Later when Harry took the hawthorn wand from Draco, the Elder Wand knew that Draco had been "beaten" and it changed its allegiance to Harry Potter. It was simply waiting for him to gain possession of it.
But that doesn't make any sense. According to Ollivander, the WAND has to be won from its owner, in order for it to change allegiance. Harry didn't win the Elder Wand; he won the hawthorn wand. If this wasn't the rule, no one would want to win another's wand. If he lost it or gave it back, he'd lose his own too (lose one of your wands means losing all of them to the winner). Capturing another's wand increases your vulnerability to your wand switching allegiance. If you ask me it makes more sense that the Elder Wand refused to attack Harry because it thought Draco was holding the hawthorn wand.
Look at it this way; it was taken from the hand of it's owner by the holder of the hawthorn wand (Draco). That wand was taken from it's owner by Harry. Even though it was not taken directly by Harry, the chain of dominance remains unbroken -- Harry defeated Malfoy who defeated Dumbledore. The wand simply recognizes when it's owner is defeated and gives it's allegiance to the victor; the fact that it was never in his hand is irrelevant -- the wand 'knows'.... It seeks the 'best' to be held by, and it determines this by who wins. Lose while you have it's allegiance, and it serves the winner.
@Jay (Moving out of Universe) There are many many inconsistencies in HP if you start looking too hard, a degree of suspension of disbelief is necessary.
@Richard Sure there are inconsistencies, but this is the most crucial one, since it's the key plot point that allows Harry to defeat Voldemort. And TangoOversway, I don't think JKR would want to bring the series to an end with a lazy deus ex machina like "it's magic". I'm not completely satisfied with any of the answers but it at least proves that no one really has a clue and I don't feel alone anymore. :)
I would take it that the Elder Wand is an exception. It specifically allies with whoever has defeated the last master, under any circumstances. Even in the story, one wizard beat the other by stabbing him with a knife. That means the Elder Wand changed allegiances through a totally non-magic method, which is way stranger than it changing allegiances because someone was beaten magically using a different wand.
The Elder Wand is cursed. That is why it doesn't require its wielder to be beaten while it is used (as most wands do). It only requires that its wielder be beaten in some fashion.