There is a verse that says, “If a person sins but does not know it, he is guilty and bears his iniquity” (Lev. 5:17). Rashi’s chiddush (his novel idea) is that just as a person is punished if he sins unwittingly, so is he rewarded if he does a good deed unwittingly (as usual with Rashi, the doctrine derives from much earlier rabbinic sources, but such is Rashi’s greatness that without him we may not have known it).
An example given by Rabbi Elazar ben Azariah of this unwitting mitzvah concept is a person who is harvesting his field and forgets a sheaf. The Torah prescribes (Deut. 24:19) that the stranger, the orphan, etc., may enter and take the forgotten sheaf. This is part of the Jewish code of social welfare and ensures that no-one will be without means of support.
We are assured that the Holy One, blessed be He, has a special blessing for the farmer, yet the farmer was unaware that he had done a mitzvah. Had he consciously decided to do the mitzvah, we could not have said that he had forgotten the sheaf. Where would the forgetfulness have been if he knew what he was doing? The result of his unwitting good deed is that he enjoys a Divine blessing.
One should never do a mitzvah for the sake of a reward but if the reward comes, it is still a reward. It is always best to do a mitzvah because you want to, but if the mitzvah happens regardless, it is still a mitzvah.
There is a Biblical verse, “Cast your bread upon the waters, for you will find it after many days” (Kohelet 11:1). The conventional interpretation is that if you invest wisely you will eventually gain a profit, but the Soncino edition of Kohelet adds (page 181), “The traditional Jewish interpretation is accepted by many moderns, that the exhortation is to practise goodness and kindness from which a reward may unexpectedly and after a long interval be reaped”.
In the sense of the Rashi commentary which we have quoted, the verse may be read as saying, “Make good deeds a habit and sometimes you will find yourself doing them even without realising it, and even if you were not looking for a reward you will still earn one”.