John lends his phone to Sherlock. Not the other way around.
But when John meets Mycroft, Sherlock texts John.
1) Sherlock texted [or emailed] Lestrade and wanted him to send the number "of the phone I just texted you from" and perhaps made some excuse about misplacing the number. Or knowing Sherlock, no excuse at all.
2) Sherlock asked Mike Stamford for the number. Probably by text. :)
3) John himself called Sherlock and left a message, maybe before leaving for Baker Street. John is the courteous sort. He might have felt it best to confirm with Sherlock, and when John didn't get a reply, decided that he might as well go anyway.
This is more likely than John texting Sherlock. Which is possible and not completely improbable.
But when John types up a text later, he is very slow -- as when he types elsewhere in the show, in fact. Texting is probably not the first thing he would think of. How long has he had Harry's phone?
John might not remember that Sherlock tells Mike "I prefer to text."
4) Sherlock asked Mycroft for the number. Least likely of all.
Mycroft probably used his cameras to ID John Watson -- if not Mycroft's source[s] inside the Yard, which I do not doubt MH possesses, and no, this is not an argument for Mystrade, sorry.
Tangent: Odds are MH did that not long after Sherlock and John both left Bart's.
Although considering that Mycroft had got the notebook from John's therapist -- fast work, that! -- by the time John was leaving the crime scene... and Mycroft had had time to read her notes over too. Along with John's dossier.
In Baker Street, Sherlock and John do not exchange numbers. We see everything they say and do. Just like when they're meeting at Bart's.
So is this another example of Sherlock's ability to hack into people's phones? Or something else?