The punishment of lashes serves as a kapparah (atonement) for a person's aveirah.
1) The Sefer HaChinuch (Mitzvah # 594) writes that the reason for the mitzvah of lashes was to penalize the sinner in this world so that he should change his ways and still merit a desirable afterlife.
2) Rambam Hilchos Sanhedrin 17:7- Whenever a person sins and is lashed, he returns to his original state of kashrus.
see also: Rashi on Sanhedrin 10a who writes how lashes are like one of the 4 types of capital punishment (ie each which provides kapparah)
The mishnah on Makkos 22b states:
נתקלקל בין בריעי בין במים פטור רבי יהודה אומר האיש בריעי והאשה במים:
If one sullies himself, whether with excrement or with urine, he is exempt [from further lashes]. Rabbi Yehuda says: the man is exempted with excrement, and the woman is exempted with urine.
expanded on Makkos 23a
... and the Rambam elaborates further in Hilchos Sanhedrin 17:5
מִי שֶׁאֲמָדוּהוּ וּכְשֶׁהִתְחִיל לִלְקוֹת נִתְקַלְקֵל מִכֹּחַ הַהַכָּאָה בֵּין בְּרֵיעִי בֵּין בְּמֵימֵי רַגְלַיִם אֵין מַכִּין אוֹתוֹ יוֹתֵר שֶׁנֶּאֱמַר (דברים כה ג) "וְנִקְלָה אָחִיךָ לְעֵינֶיךָ" כֵּיוָן שֶׁנִּקְלָה פָּטוּר
When it was estimated that a person could bear a specific number of lashes, they began lashing him and he became distressed due to the power of the blows and either defecated or urinated, he is not given any more lashes. This is derived from Deuteronomy 25:3: "and your brother will be degraded before your eyes." Since he was distressed, he is absolved.
I understand how wetting or soiling oneself is a fulfillment of "and your brother will be degraded before your eyes" and is enough to cut the session short.
But can it really be that a person receives the kapparah for lashes by wetting or soiling themself? How?