Get him to a pediatric gastroenterologist (referred by your pediatrician) and get a full workup for encopresis. It is true that the bowel material slips out without the child being aware of it, because the lower intestines are not functioning correctly. Get him seen soon, because it can be very devastating to a child to be in this position.
I agree with AnnieBrook.
Make an urgent appointment for him to be seen by the doctor for referral to the specialist with regard to the bowel problem.
He may have the problem that AnnieBrooke mentions. He may not realise what the sensations are when his bowels are about to open, or he deliberately holds on to the point that the bowels open and he then has no control.
With regard to the wetting, that needs to be investigated also. But I suggest that you do NOT give him any drinks for 3 hours before bedtime.
You can also rouse him in the middle of the night or before you go to bed to take him to the toilet to empty his bladder.
Some children sleep very deeply and may also dream that they are on the toilet.
If he has had a time when he was dry at night time, he may have a urinary infection. Make sure that you teach him how to gently pull back his foreskin to wash to avoid getting infections.
Let us know how you get on.
Best of luck and best wishes.