Two transiting planet candidates with super-Earth radii around the nearby K7--M0 dwarf star TOI-1238 were announced by TESS. We aim to validate their planetary nature using precise radial velocities (RV) taken with the CARMENES spectrograph. We obtained 55 CARMENES RV data that span 11 months. For a better characterization of the parent star's activity, we also collected contemporaneous optical photometric observations and retrieved archival photometry from the literature. We performed a combined TESS+CARMENES photometric and spectroscopic analysis by including Gaussian processes and Keplerian orbits to account for the stellar activity and planetary signals simultaneously. We estimate that TOI-1238 has a rotation period of 40 $\pm$ 5 d based on photometric and spectroscopic data. The combined analysis confirms the discovery of two transiting planets, TOI-1238 b and c, with orbital periods of $0.764597^{+0.000013}_{-0.000011}$ d and $3.294736^{+0.000034}_{-0.000036}$ d, masses of 3.76$^{+1.15}_{-1.07}$ M$_{\oplus}$ and 8.32$^{+1.90}_{-1.88}$ M$_{\oplus}$, and radii of $1.21^{+0.11}_{-0.10}$ R$_{\oplus}$ and $2.11^{+0.14}_{-0.14}$ R$_{\oplus}$. They orbit their parent star at semimajor axes of 0.0137$\pm$0.0004 au and 0.036$\pm$0.001 au, respectively. The two planets are placed on opposite sides of the radius valley for M dwarfs and lie between the star and the inner border of TOI-1238's habitable zone. The inner super-Earth TOI-1238 b is one of the densest ultra-short-period planets ever discovered ($\rho=11.7^{+4.2}_{-3.4}$ g $\rm cm^{-3}$). The CARMENES data also reveal the presence of an outer, non-transiting, more massive companion with an orbital period and radial velocity amplitude of $\geq$600 d and $\geq$70 m s$^{-1}$, which implies a likely mass of $M \geq 2 \sqrt{1-e^2}$ M$_{\rm Jup}$ and a separation $\geq$1.1 au from its parent star.