The Pharisees strictly kept the Law and believed it was wrong to heal on the Sabbath yet, rather bizarrely, they didn't have any qualm about plotting murder. "Then he (Jesus) said to the man, 'Stretch out your hand.' So he stretched it out and it was completely restored … But the Pharisees went out and plotted how they might kill Jesus" (Matthew 12:13-14).
Jesus, like David, before him knew when it was appropriate to break the rules. In 1 Samuel 21:1-6 David ate the consecrated bread which it was only lawful for priests to eat and he gave some to his companions. Jesus comments on this in Luke 6:3-4. David understood God’s purposes in giving the law and he knew God's heart. Something the Pharisees consistently failed to understand.
Likewise Jesus did many things that were technically against the Law. He touched lepers, broke the Sabbath and forgave sinners – instead of stoning them. Jesus knew when and where to break the rules because he lived in a dependant relationship with God.
The Law was only ever intended to lead us to Christ, "So the law was put in charge to lead us to Christ that we might be justified by faith" (Galatians 3:24). Yet so often the Law has been used to drive people away. People who felt they could never measure up, who felt they had failed too badly or were too tired to try anymore. The Law was meant to lead us into the same dependant relationship that Jesus had with God. So that we would be "doing the will of God from your heart" (Ephesians 6:6) and not from a rule book.
God wants us to know his heart so we can do his will without having to refer to a set of rules.