Pedro Hernandez, 69, agreed to serve 12 years in state prison in a plea deal with prosecutors, district attorney's spokesman Seth Steward said.
Hernandez pleaded guilty to one count of continuous sexual abuse of a minor following his arrest in June in connection with the sexual assault of the girl on at least three separate occasions at Sanchez Elementary School, police and prosecutors said.
On June 3, a teacher's aide at the school became suspicious of Hernandez's behavior toward the girl after he arrived to bring her lunch around noon, police said.
She later caught Hernandez assaulting the girl in a secluded stairwell on campus and called police. The sexual assault was recorded on the school's video surveillance system, according to police.
Hernandez left before officers arrived but was arrested a week later at a homeless shelter in San Bruno.
Prosecutors said Hernandez, who collected recyclables for a living, had been staying with the girl's family at the time of the assaults.
He originally pleaded not guilty to seven felony counts, including sexual intercourse or sodomy with a child, and oral copulation or sexual penetration with a child, and faced a maximum sentence of life in prison before agreeing to the plea deal on Monday, prosecutors said.
Hernandez is scheduled to be formally sentenced on April 12.