Trees generally take in carbon dioxide and release oxygen during photosynthesis, but in addition, they respire. For respiration, oxygen is used and plants not only synthesize their foods they break them down during respiration with carbon dioxide given out as waste. All living things respire, remember.
In a swamp, the soil is waterlogged with low oxygen content, such plants, not only mangroves, adapted breathing roots to cope with respiration.
Plants usually take up oxygen through their roots and release CO2. Some plants develop aerenchyma to transport oxygen from the atmosphere to their roots. Mangroves are known to have extensive aerenchyma for this purpose. You will find an extensive literature on this. Just put aerenchyma and mangroves into google scholar or another search engine.
This is a common phenomenon in plants for carrying out their respiratory requirement and survival. Especially, mangroves take up oxygen like other anaerobic plants for aerobic respiration and also other functions deep in the rhizospheric zone.
Article Mangrove root: adaptations and ecological importance