Daniel Cieslak, 21, had sex with the youngster at a flat in Gorgie after meeting her at a taxi rank.
The youngster had previously been spotted in the city centre by police officers searching for a missing person in the early hours – but a judge heard they were “not concerned by her age”.
Cieslak was held after the girl’s sister reported what had happened.
The then-engineering student sobbed when officers revealed how old the child was – after he thought she was 16.
Cieslak now faces jail after he pleaded guilty to a rape charge at the High Court in Glasgow.
His bail was continued as sentencing was deferred until next month.
The youngster and a teenage friend had been in Edinburgh city centre on July 31, 2015.
In the early hours of the next morning, police were in Princes Street following a report of a missing girl.
The 12-year-old happened to direct officers to the child they were looking for.
Despite the time, prosecutor Kath Harper explained: “Police spoke to (the girl who was raped), but did not note her details as they were not concerned by her age.”
The girl and her friend were later in taxi queue when they got talking to Cieslak and his pal.
They all got in a cab together and headed to the flat in Gorgie.
It was there Cieslak and the 12-year-old began kissing before the Polish-born student had sex with her.
Days later, the youngster told her sister she was “extremely worried” she could be pregnant.
Miss Harper told the court: “She was distressed at this time.”
The 12-year-old’s GP “encouraged” her to go to the police. It was the girl’s sister who reported the incident.
Cieslak, of Leith, later admitted he had sex with the girl, but denied it was rape.
He said he was told the girl was 16.
Prosecutor Miss Harper said: “When it became clear police were suggesting she was younger, he asked them what age she was. When told she was 12 years-old, he started to cry and held his head in his hands.”
Cieslak had been due to start an Architectural Technology course at Napier University at the time. He had been studying engineering.
It’s understood he worked at The Dome in George Street until recently.
His lawyer David Nicolson said Cielsak “genuinely believed” the girl was older.
Lady Scott adjourned sentencing for reports.