Great question! But I think we have to understand that children are a gift from God. Jesus was a blessing to Mary (as we say in the prayer) and Joseph as our children are to us. They are not ours, but rather God’s from the beginning - they are on ‘loan’ to us if you will. We must teach them to love God first before us as parents, yet we will always feel that burden of care for them - just like Mary and Joseph had concern for Him and went searching for Him.
In another instance, Jesus explains how we are to follow Him, not stopping to say goodbye to our parents or other loved ones. Anyone that loves anyone or anything more than Jesus is not worthy of Him. Therefore, Jesus being perfect and loving the Father, could not have stayed behind with Mary and Joseph even if some people think his action contradicted the fifth commandment. It did not. In fact, by going to the temple, Jesus, in this instance, fulfilled the greatest commandment of all - to love God with all your heart and mind. As the Son, he had to obey the Father first just as His followers, we must obey Him first above all else. And yes, this may sometimes cause conflict within the family unit - but did our Lord not also say that he came to bring a sword and that sometimes this would turn son against father and daughter against mother? This is the mystery that we can only be at peace with if the Spirit is with us. I hope it is with all of us.