We report on two fossil groups of galaxies at redshifts z= 0.425 and 0.372 discovered in the Cosmic Evolution Survey (COSMOS) area. Selected as X-ray extended sources, they have total masses (M200) equal to 1.9(±0.41) × 1013 and 9.5(±0.42) × 1013 M⊙, respectively, as obtained from a recent X-ray luminosity–mass scaling relation. The lower mass system appears isolated, whereas the other sits in a well-known large-scale structure (LSS) populated by 27 other X-ray emitting groups. The identification as fossil is based on the i-band photometry of all the galaxies with a photometric redshift consistent with that of the group at the 2σ confidence level and within a projected groupcentric distance equal to 0.5R200, and iAB≤ 22.5 mag limited spectroscopy. Both fossil groups exhibit high stellar-to-total mass ratios compared to all the X-ray selected groups of similar mass at 0.3 ≤z≤ 0.5 in the COSMOS. At variance with the composite galaxy stellar mass functions (GSMFs) of similarly massive systems, both fossil group GSMFs are dominated by passively evolving galaxies down to Mstars∼ 1010 M⊙ (according to the galaxy broad-band spectral energy distributions). The relative lack of star-forming galaxies with 1010≤Mstars≤ 1011 M⊙ is confirmed by the galaxy distribution in the b−r versus i colour–magnitude diagram. Hence, the two fossil groups appear as more mature than the coeval, similarly massive groups. Their overall star formation activity ended rapidly after an accelerated build up of the total stellar mass; no significant infall of galaxies with Mstars≥ 1010 M⊙ took place in the last 3 to 6 Gyr. This similarity holds although the two fossil groups are embedded in two very different density environments of the LSS, which suggests that their galaxy populations were shaped by processes that do not depend on the LSS. However, their progenitors may do so. We discuss why the late merging of a compact group is favoured over the early assembly as a formation scenario for the isolated, low-mass fossil group.