Downpatrick Crown Court heard Brandon John Rainey, 18, had "groomed" his victim with flattery, cannabis and prescription drugs.
Rainey pleaded guilty to charges of rape, attempted rape and sexual assault, all on 1 April last year.
The judge said it was clear Rainey knew his victim was 12.
The court heard the girl's mother caught the pair in bed together after they had exchanged social media messages.
It was claimed the mother physically assaulted Rainey before calling police.
Rainey, whose address was given as c/o the Young Offenders Centre, denied any wrongdoing when interviewed by police, but when DNA evidence was put to him, he claimed the girl told him she was 16.
Jailing him, the judge said it was clear that Rainey knew at all times his victim was a child of 12 and that even after he had been caught and assaulted by his victim's mother, he continued to send the girl messages "to try to avoid detection".
Rainey also told her to clear and delete any previous messages he had been sent.
The judge said that as well as being under a suspended sentence at the time of the rape, Rainey had been in court that day and had been made the subject of a year-long conditional discharge for public order offences.
It was because of his record, his attitude to the offence and because of his behavioural and mental health problems coupled with a history of "substance misuse and abuse" that the judge said he was deeming Rainey to be a "significant risk to the public".
The judge said he believed the public would be best protected by an extended sentence, meaning that when eventually released by the Parole Commissioners, Rainey would have to serve an extra two years on supervised licence.
Rainey was also barred from working with children, ordered to sign the police sex offenders register and made the subject of a 10 year Sexual Offences Prevention Order.
A teenage rapist who was caught in bed with a 12-year-old girl by her mother has been given a four-year custodial sentence.