This question was asked by many authorities. For example, the Chacham Tzvi § 49.
He says that there are those that suggest it was because of their intellectual maturity. Even though they were minors, they were mentally responsible for their actions like an adult. The Dovev Mesharim ed. of the Chacham Tzvi points to the Sefer Chassidim (Makitzei Nirdamim ed., based on Italian manuscripts) § 217 who says this answer (you can also see a different version quoted in @Alex's answer).
קטן שחילל שבת אינו נהרג וקטן שהורה לפני רבו חייב מיתה כשמואל לעלי, אלא כשאמרה חנה (ש"א א כז) אל הנער הזה התפללתי, הרי משמים מענישין קטן הואיל ודעתו כגדול כער ואונן
However, the Chacham Tzvi rejects this approach. Instead, he wants to limit the scope of this concept. He gives four approaches on how to limit it (see here for a summary), as he is bothered how could someone commit crimes as a minor and not be punished in the world to come. However, three of them don't address Er and Onan. The one that does is that the concept that Heavenly punishment is only above twenty is that we sometimes see this to be true.
Someone I found who tries to use the Chacham Tzvi's rejected approach is Rav Dovid Kronglass (former Mashgiach at Yeshivas Ner Yisroel) in Sichos Chochmah UMussar II § 50. He builds off the Chavos Yair § 166, who explains this concept to only apply to "mitzvos sichliyos", intuitive mitzvos. Something someone can figure out on their own, they're punished for, even as a child. He gives the example of machlokes, where we see the babies of Korach and his assembly were punished. This is pointed out in various sources:
Bamidbar Rabbah 18:4; Midrash Tanchuma Korach § 3:
כַּמָּה קָשָׁה הַמַּחֲלֹקֶת, שֶׁבֵּית דִּין שֶׁל מַעְלָה אֵין קוֹנְסִין אֶלָּא מִבֶּן עֶשְׂרִים שָׁנָה וָמַעְלָה, וּבֵית דִּין שֶׁל מַטָּה מִבֶּן שְׁלֹשׁ עֶשְׂרֵה. וּבְמַחֲלֻקְתּוֹ שֶׁל קֹרַח, תִּינוֹקוֹת בֶּן יוֹמָן נִשְׂרְפוּ וְנִבְלְעוּ בִּשְׁאוֹל תַּחְתִּית
How harmful is dissension! An emissary of the court on high only imposes a penalty [on people] from the age of twenty years, while the court below [only imposes a penalty] from the age of thirteen years. In the case of Korah's dissension, however, one-day-old babies were burned and swallowed up in nethermost Sheol
Machlokes is intuitively negative. However, prohibitions that are explicit are equally punished by Heaven and Earth starting at the ages of 12/13. Rav Kronglass says the Chasam Sofer (YD 155) writes somewhat similar to this:
בשרה נאמר בת עשרים כבת ז' בלא חטא התם קודם מ"ת הוה והכל אצלה כמצות שכליות וע"ז נאמרו שיעורי' הללו
As well, Rav Kronglass points out that this explains why children died during the flood, and why children in Sedom were punished. Both those events involved mitzvos sichliyos.
Rav Kronglass takes this differentiation and uses it to explain Er and Onan. He says that it can't be that their sin was part of the mitzvos sichliyos, so that means someone thirteen and above should be punished by Heaven. However, as you pointed out, they were even younger than that. He says that their intelligence was that of someone over the age of thirteen, so they were punished by Heaven by that standard.