I've had sort of similar things happen to me before.
It takes a little bit of charm, keeping calm, and delivering the work.
Usually, if you can get to the teacher who it was in to, prior to being told about this after school detention, then you can get away with it.
Otherwise, just go to it. It's only, what, a half hour, an hour?
If you've got the work anyway, and you're in the after school to do it, then maybe you can explain that you have it and you simply forgot.