We present the results of high-quality long-slit spectroscopy of planetary nebulae (PNe) and H II regions in the two dwarf irregular (dIrr) galaxies Sextans A and B, which belong to a small group of galaxies just outside the Local Group. The observations were obtained with the New Technology Telescope ESO Multi-Mode Instrument. In Sextans A we obtained the element abundances in its only known PN and in three H II regions with the classical Te method. The oxygen abundances in these three H II regions of Sextans A are all consistent within the individual rms uncertainties, with an average 12 + log (O/H) = 7.54 ± 0.06. The oxygen abundance of the PN in Sextans A is, however, significantly higher: 12 + log (O/H) = 8.02 ± 0.05. This PN is even more enriched in nitrogen and helium, suggesting a classification as a PN of type I. The PN abundances of S and Ar, which are presumably unaffected by nucleosynthesis in the progenitor star, are well below those in the H II regions, indicating lower metallicity at the epoch of the PN progenitor formation (∼1.5 Gyr ago, according to our estimates based on the PN parameters). In Sextans B we obtained spectra of one PN and six H II regions. Element abundances with the Te method could be derived for the PN and three of the H II regions. For two of these H II regions, which have a separation of only ∼70 pc in projection, the oxygen abundances do not differ within the rms uncertainties, with a mean of 12 + log (O/H) = 7.53 ± 0.05. The third H II region, which is about 0.6 kpc northeast of the first two, is twice as metal-rich, with 12 + log (O/H) = 7.84 ± 0.05. This suggests considerable inhomogeneity in the present-day metallicity distribution in Sextans B. Whether this implies a general chemical inhomogeneity among populations of comparable age in Sextans B, and thus a metallicity spread at a given age, or whether we happen to see the short-lived effects of freshly ejected nucleosynthesis products prior to their dispersal and mixing with the ambient interstellar medium will require further study. For the PN we measured an O/H ratio of 12 + log (O/H) = 7.47 ± 0.16, consistent with that of the low-metallicity H II regions. We discuss the new metallicity data for the H II regions and PNe in the context of the published star formation histories and published abundances of the two dIrr galaxies. Both dIrrs show generally similar star formation histories in the sense of continuous star formation with amplitude variations but differ in their detailed enrichment timescales and star formation rates as a function of time. If we combine the photometrically derived estimates for the mean metallicity of the old red giant branch population in both dIrrs with the present-day metallicity of the H II regions, both dIrrs have experienced chemical enrichment by at least 0.8 dex (lower limit) throughout their history.