Context. The spectral shape of the X-ray emission in solar flares varies with the event size, with small flares generally exhibiting softer spectra than large events, indicative of a relatively lower number of accelerated electrons at higher energies. Aims. We investigate two microflares of GOES classes A9 and C1 (after background subtraction) observed by STIX onboard Solar Orbiter with exceptionally strong nonthermal emission. We complement the hard X-ray imaging and spectral analysis by STIX with co-temporal observations in the (E)UV and visual range by AIA and HMI to investigate what makes these microflares so efficient in high-energy particle acceleration. Methods. We made a preselection of events in the STIX flare catalog based on the ratio of the thermal to nonthermal quicklook X-ray emission. The STIX spectrogram science data were used to perform spectral fitting to identify the non-thermal and thermal components. The STIX X-ray images were reconstructed to analyze the spatial distribution of the precipitating electrons and the hard X-ray emission they produce. The EUV images from SDO/AIA and SDO/HMI LOS magnetograms were analyzed to better understand the magnetic environment and the chromospheric and coronal response. For the A9 event, EOVSA microwave observations were available, allowing for image reconstruction in the radio domain. Results. We performed case studies of two microflares observed by STIX on October 11, 2021 and November 10, 2022, which showed unusually hard microflare X-ray spectra with power-law indices of the electron flux distributions of δ = (2.98 ± 0.25) and δ = (4.08 ± 0.23), during their non-thermal peaks and photon energies up to 76 keV and 50 keV, respectively. For both events under study, we found that one footpoint is located within a sunspot covering areas with mean magnetic flux densities in excess of 1500 G, suggesting that the hard electron spectra are caused by the strong magnetic fields the flare loops are rooted in. Additionally, we revisited a previously published unusually hard RHESSI microflare and found that in this event, there was also one flare kernel located within a sunspot, which corroborates the result from the two hard STIX microflares under study in this work. Conclusions. The characteristics of the strong photospheric magnetic fields inside the sunspot umbrae and penumbrae where flare loops are rooted play an important role in the generation of exceptionally hard X-ray spectra in these microflares.