The Head Boy/Girl are chosen by the Headmaster/mistress and do not have to be Prefects, though they can be.
From the HP Wiki:
At the beginning of the school year, the Headmaster appoints new
prefects, as well as one Head Boy and one Head Girl from all of the
seventh year students. These students are chosen because of their
academic achievement, outstanding reputation as a student, and an
honest, good, and hard-working personality. It should be noted that
being a prefect is not a prerequisite to being appointed a Head Boy or
A prefect at Hogwarts School of Witchcraft and Wizardry is a student
who has been given extra authority and responsibilities by the Head of
House, Headmaster or headmistress. One male and one female student are
chosen from each house in their fifth year to act as prefects, and
would continue to be prefects in their sixth and seventh years until
they leave school. Thus, there are approximately six prefects per
house, and twenty-four prefects in the whole school at one time. The
Head Boy and Head Girl may be chosen from among this number and may
not be replaced, but, like with James Potter, the Head Boy or Girl
does not have to be a prefect.
The Prefects are chosen in their fifth year, and the Head Boy/Girl are chosen in their seventh.
Two years is a long time, especially for teenagers. People can mature a lot between 15 and 17 (or 14 and 16, for those with late birthdays).
Look at the qualities for Head Boy: "academic achievement, outstanding reputation as a student, and an honest, good, and hard-working personality". This certainly describes James Potter in his seventh year. He had always had a reputation, and had always been an achiever. That said, he had not demonstrated, by his fifth year, that he was a good candidate for added 'responsibilities and authority'. In that year he was still bullying, still hanging Severus in the air by his ankle, and still portraying himself as a ruffian.
By his seventh year, this had mostly changed. He (mostly) stopped bullying, and started helping. He expanded his circle of friends beyond the Marauders, and began displaying the qualities he'd show for the rest of his short life.
The people who are chosen as prefects are people who display many of the qualities a Head Boy (or Girl) should have, but not everyone is suited to wielding power or authority. Some people take it and let the expression of power become the focus of their life (like Malfoy). These people should not be permitted added power, and the staff at Hogwarts have learned to recognize those who have failed to live up to their potential. Putting someone who used their power as a weapon in charge of the rest of the prefects would serve to institutionalize that mindset, and the teachers know this.
If, by some rotten luck, all 4 prefects of a given gender in a year were to fail in this way, the staff can chose someone outside of that limited sampling to become Head Boy or Girl. It also gives people who have demonstrated growth the chance to demonstrate it - they can still work hard and be recognized for it.