These verses have long confounded me, given my understanding of them. Maybe someone can set me straight.
The Pharisees' object wrongly to the disciples plucking grain with their hands. Deut 23:25 allows this, but the Pharisees added to Scripture in forbidding it, reckoning it as work not to be done on the Sabbath. This reckoning is not in Scripture.
In reply, Jesus gives an example of when David actually went against the written law (Lev 24:5-9) and was not held guilty, since it was a special circumstance.
In essence, Jesus says: look, not only is it okay for them to pluck grain, against your extra-Scriptural ruling, it was even okay for David to eat the tabernacle bread, against the Scriptural law.