Context. Characterizing the response of the upper Martian atmosphere to solar flares could provide important clues as to the climate evolution of the red planet in the early Solar System, when the extreme ultraviolet and soft X-ray radiation was substantially higher than the present-day level and when these events occurred more frequently. A critical process herein is the Martian atmospheric escape in the form of atomic C and N, as mainly driven by CO2/CO and N2 dissociation. Aims. This study is devoted to evaluating how these escape rates varied on the dayside of Mars during the X8.2 solar flare on 10 September 2017. Methods. The background Martian atmospheric structures, before, during, and after the flare, are constructed from the Neutral Gas and Ion Mass Spectrometer measurements made on board the Mars Atmosphere and Volatile Evolution spacecraft, from which the hot C and N production rate profiles via different photon and photoelectron impact channels and on different flare stages are obtained. They are combined with the respective escape probability profiles computed using a test particle Monte Carlo approach to derive the atomic C and N escape rates on the dayside of Mars. Results. Our calculations indicate that the pre-flare C and N escape rates are (1.3−1.4) × 1024 s−1 over the dayside of Mars. During the event, we find a modest decrease in the C escape rate of 8% about 1 h after the flare peak, followed by a recovery to the pre-flare level several hours later. However, an opposite trend is found for the N escape rate during the same period, which shows an increase of 20% followed by a recovery to the pre-flare level. Conclusions. The distinction between C and N in terms of the variation in the escape rate during the solar flare reflects the competition between two flare-induced effects: enhanced hot atom production via dissociation and enhanced collisional hindrance due to atmospheric expansion.