As our prophet Mohammad was set to be the last messenger, it was decided by God that his sons dies, because if they remained alive they would be prophets (our belief).
Some one may say but God could have given him only daughters, well I will say yes but maybe God wanted to teach our prophet patience.
That is my thinking.