In Hilchos Kriyas Shema, the SA (OC 71:1) says
מִי שֶׁמֵּת לוֹ מֵת שֶׁהוּא חַיָּב לְהִתְאַבֵּל עָלָיו, אֲפִלּוּ אֵינוֹ מֻטָּל עָלָיו לְקָבְרוֹ, פָּטוּר מִקְּרִיאַת שְׁמַע וּמִתְּפִלָּה.
Someone whose relative died, one who they must mourn for, even if the body isn't their responsibility to bury, they are exempt from reciting the Shema and praying.
They're exempt from other mitzvos as well (Mishnah Berurah s.k. 4).
(ד) ומתפלה - וכן מכל הברכות אפילו ברכת הנהנין ומכל מצות האמורות בתורה
From prayer - and also all other blessings, even blessings on items of pleasure, and from all mitzvos in the Torah.
The Mishnah Berurah s.k. 3 adds an important detail:
(ג) פטור וכו' - ואם קרא אינו יוצא וצריך לחזור ולקרות אחר הקבורה. [חידושי רע"א]:
And if you recited shema, you haven't fulfilled your mitzvah and you have to recite it again after the burial
We see from here that someone who is exempt from a mitzvah, doing the act isn't considered doing the mitzvah, so they haven't fulfilled the obligation (meaning if they become obligated later they can't rely on what they did earlier).
However, in Hilchos Pesach the SA (OC 475:5) says
אָכַל כַּזַּיִת מַצָּה וְהוּא נִכְפֶּה, בְּעֵת שְׁטוּתוֹ, וְאַחַר כָּךְ נִתְרַפֵּא, חַיָּב לֶאֱכֹל אַחַר שֶׁנִּתְרַפֵּא, לְפִי שֶׁאוֹתָהּ אֲכִילָה הָיְתָה בְּשָׁעָה שֶׁהָיָה פָּטוּר מִכָּל הַמִּצְוֹת.
Someone who was in a state of insanity and ate matzah and later became better, they have to eat matzah again. This is because when they ate the matzah the first time, they were exempt from all mitzvos.
The Shaar HaTziyun s.k. 39 adds a case:
לאפוקי שומר אבדה או משמר המת, אף דהוא גם-כן פטור אז מכל המצות [ואפילו יכול לקיים שניהם, אם צריך לטרוח אחר זה]...אם אכל אז מצה יצא ידי חובתו, דהוא איש, אלא שאז לא חייבתו התורה מפני שהוא עוסק במצוה אחרת.
Unlike someone guarding a lost object or a dead body, even though they are also exempt from all mitzvos (even if they could fulfill both, in the case that they have to add exertion to fulfill the second mitzvah)...if they ate matzah at that point, they have fulfilled their obligation. This is because they are a normal person (unlike someone deranged), just the Torah exempted them because they're doing a different mitzvah.
I see a glaring contradiction here. Someone exempt from a mitzvah who does it anyways, did they fulfill it or not? You could argue one is a case of אונן and one עוסק במצוה, but I don't see the distinction. Besides, I believe an onen is exempt because they have the status of one who is עוסק במצוה, (I found the Magen Avraham says this, although Rashi in Berachos says onen is equivalent to a chasan on his wedding night, טרוד במצוה which is related to עוסק במצוה).
Edit: I found another instance where the Mishnah Berurah (70:4 s.k. 18) says someone עוסק במצוה, if they recite Shema, they've fulfilled their obligation. So this seems to be a rule with עוסק במצוה. The only question then is how to differentiate an אונן. Although now I'm wondering if it makes a difference that the person stopped the mitzvah they were עוסק in, and then did the other mitzvah.