He said to them... Here the justification is given: first, with certain examples; secondly, with a text of Scripture (v. 7).
In regard to the first he does two things: first, he gives an example, in which some were excused on account of necessity; secondly, in which some were excused on account of holiness (v. 5).
He says, therefore, He said to them... In Leviticus (24:5) it is read that twelve loaves were baked from fine flour and placed on the table of proposition on the Sabbath, and on another Sabbath they were removed and others put there, and the first ones were eaten by the sons of Aar...on. Then in 1 Samuel (21:6) it is written that when David was fleeing from Saul, Ahimelech divided those loaves among them. This is why he says, Have you not read what David did, when he was hungry, and those who were with him. For David was a good man, of whom the Lord says that he has found a man according to his own heart (1 Sam 13:8). But someone might say that David was a prophet and so was allowed to take them. Therefore, he adds, and those who were with him. The loaves of proposition were the ones offered on the Sabbath and it was not lawful according to the precept to eat them (Lev 24:5). But what does this have to do with the case? Because it was the Sabbath, when he did this. And this is clear, because he said, "I have no bread except what I took form the Lord's table," and he did this only on the Sabbath. Likewise on the first day of the month was the feast of the new moon; therefore, if it happened on the Sabbath, it was violated by necessity. But it still seems that he did not violate it, because it is not a sin to eat on the Sabbath. However, Chrysostom says that he violated more than the Sabbath, because he took loaves, which were not lawful for anyone to use, out of necessity. Likewise, it should be noted that according to Chrysostom some precepts are such that they are commanded for their own sake, and these cannot be broken for any need; but some are not for their own sake but for the sake of a figure, and these can be broken at certain times and places: as a fast can now be broken out of necessity. But that bread was the figure of another bread, namely, the bread of the altar, which is received not only by the priest but by the people; therefore, David was the figure of that people. Hence Revelation (5:10): "You have made us a kingdom and priests to our God."
Source: Commentary on Matthew