Laws are written by people, in part, to enshrine ethics.
No, not all laws are about ethics, or even about what is just and right. But that's the basis of the system.
It's unethical to commit statutory rape, and people have agreed with this sentiment enough to make it very illegal, to the point that you risk jail time for committing it.