Christ himself said, "Give to Caesar what is Caesar's...". Clearly the law meant something.
Yes, Joseph and Mary were obeying the law. They were not “illegal aliens” as many try to say. Later, when they went to Egypt, it was because an angel told them to, and they went back home as soon as it was safe.