The incident happened Wednesday when Mariano Rajoy was mingling with crowds in Pontevedra city in his northwestern fiefdom of Galicia on the latest stop of his push for re-election in Sunday general elections.
The teenager approached and hit the prime minister hard on the left side of his face, knocking off and breaking his glasses before being wrestled to the ground by Rajoy's bodyguards.
A photo posted after the incident pictured Rajoy without his glasses, red marks on the left of his face and neck, but the premier was not badly hurt and continued his walk through the city.
It emerged that the attacker was actually a relative of Mariano Rajoy's wife, a privately educated boy named locally as Andrés de V. F, from a respected Pontevedra family.
The High Court of Galicia announced Thursday that a court in Pontevedra had sent the teenager to a youth detention centre.
"He will be locked up in the centre... for a maximum period of six months," it said, awaiting a final ruling on the case by a juvenile court in Pontevedra.
Rajoy went on to appear as planned at a campaign meeting Wednesday evening, and travelled to Brussels on Thursday for the last EU leaders' summit of his four-year term.
Spanish television showed Rajoy surrounded by Germany's Angela Merkel and Britain's David Cameron on the sidelines of the summit.
Merkel was pictured reaching out for Rajoy's left cheek, in what was interpreted as a sign of concern for the Spanish leader after the assault.