This is most probably functional constipation. Your baby has deep discomfort to the act of passing stools, so he holds it, which further makes it hard and painful to pass, hence the cycle continues.
Next Steps
Pisimpaction with proper oral dose or suppository.
In person consultation with a doctor to rule out any other condition.
Behavioural training, maintaining a stool diary.
Health Tips
Give more fluid and fibre to his diet.
Know that this problem takes a lot of time and patience to get treated, but it can be cured.