I would maybe contact the school separately now he has done his detention to see if they could 'clarify' the situation regarding library books when the pupil that has taken them out is absent from school due to unexpected major surgery (well, compared to having a verucca cut out or some of the other medical things that people might use as a medical excuse). And then clarify why exactly your son got penalised again for forgetting it on his second day back - when others have had books out for much longer.
Did he have any reminder in school either day about his book and the need to bring it back? Is it because it is end of term so they get strict to ensure all books get brought back and not lost over the holidays?
I can see why your son is annoyed - he did his first detention and half of the second one (if they were supposed to be 20 mins) and the librarian has excused him on grounds of him doing the detention well, rather than acknowledge they were unreasonable to expect him to bring a book in when he is at home recuperating from surgery.
I'd also potentially be quite annoyed at the actual detention, depending on my child's health. How well healed is your son -is he ok lifting and carrying heavy things - did he need to move or lift books in order to clean the shelves? and assuming that he is cleaning the shelves because they are dusty/dirty, dust sets off asthma attacks in ds1. So I'd be furious if he'd been randomly given a task like this to do without them checking if it had any consequences. And at 11, I don't think it is up to the child to have to say this, particularly to a teacher at the start of a detention. Some teachers are OK but there are others that are grumpy and unreasonable at the best of times; get one of those and they might just assume that the child is trying to get out of detention and the child might end up with another detention. Particularly if the child isn't particularly confident - which many aren't at that age.
It sounds like they needed help cleaning the library so figured it would be an easy source of labour to do it
and I'm not entirely against that. But given that dust can cause asthma and that not all kids are able to do all physical activities (temporarily like the dc here potentially or permanently like those kids with EDS or other conditions that could be affected) it should be the teacher that checks. If they checked these things with the child beforehand then that's one thing, if they didn't and expected the child to get on with it with no way of them getting the detention activity changed to one that didn't physically cause problems for them, then I would have a massive problem with it.
of course I have no way of knowing OP what was the case in your circumstances!