When he says to obey the law, he’s talking about the moral law, like don’t steal, kill, or commit adultery.
But when he says we’re not declared righteous by the works of the law, he’s taking about the works of circumcision, dietary laws, observing New Moons and special sabbaths, etc. things...