Mickey also contacts and talks with Riku during the ending of the first game and all of CoM, so the "how" is probably just a power he has (oddly enough, a power that Master Xehanort also uses with Terra).
Then again it's a power that Mickey never uses again, and in KH II he had to ask around for people to deliver Sora messages. I can think of a couple of explanations:
1) Somehow Mickey's "telepathy" only works when he's in the Realm of Darkness
2) Ansem's Heartless destroyed the borders between worlds, making all kinds of communication (telepathy, gummiship) easier (I think we can assume Castle Oblivion was just a special world in the Realm Between)
3) They nerfed his powers in future games to allow things to happen
About the "why"... I think he was probably following the Keyblade: he was already aware that a new Keyblade wielder was being born in that world. And maybe at the point of Sora's dream Riku was already lost as a candidate (otherwise why would Sora have that dream in the first place).
Another explanation might be that Sora was just able to attract hearts. He walks on the stained glasses of the Disney Princesses, and the stained glass usually represents that person's heart, meaning even the Princesses' hearts might've been involved in the dream.