Please take him to see your vet - no matter how experienced anyone is here we can't see the animal to give you a diagnosis.
From the sounds of it I'd put him on a course of antibiotics for this but like I said without seeing it we can't know what is actually wrong.
Let us know how it goes - if he is put on antibiotics, make sure it is a drug safe for hamsters, and it is a good idea to give some probiotic in the water too and withold green food whilst they are on medication.
Good luck