I don't know what process an M.D. goes through to enter the U.S. Navy today, but they don't go to Annapolis for four years.
Well, only a tiny fraction of people in the US Military came from one of the Service Academies. As a result, their curriculum can be somewhat ... limited.
As far as I know, none of the US Service Academies offer a pre-med degree.
Thus, a Doctor in the Armed Forces got there one of two ways:
1) he went to college, majoring in pre-med, then went to Med School, completed his residency, and then joined the Military. He will be run through something like Basic Training for officers, and then through some kind of Officer Candidate School. Since he is a Doctor, he will enter at an advanced rank (O-3, which is Captain in the Army and Lieutenant in the Navy).
2) He went to college and med school in ROTC, which means the military paid for part of his school and he spent some of his spare time getting that Basic Training and OCS. Otherwise the same as above.
I don't know if it is possible to do one's Residency in the military or not.
Dr. Julian Bashir apparently went to Starfleet Academy and majored in pre-med: he'd always wanted to be a doctor, he joined Starfleet with the intention of being a doctor. He graduated from "Starfleet Medical" as second in his class, finished his Residency (not clear if this was before or after "graduation"), and then got posted to DS9 as a Lieutenant, Junior Grade. In the modern US military, it is impossible for a doctor to rank that low if he is serving
as a doctor. (A person with an MD could, I presume, join the Navy and be an engineer instead if he so chose, and then he wouldn't get the automatic rank for being a doctor.)
So Starfleet is definitely different from how things work today.