First - have the codes checked.
Go to Autozone or some other car parts shop --- they will lend you an OBDII tester to scan the codes for you. Get the codes ... write them on a piece of paper.
Secondly, Get the codes ... and check the forums.
I've had the same problems ... and posted what my codes have shown.
Most likely ... you have one of 4 possible causes ( hopefully - not a combination).
1. A dirty MAF sensor (mass air flow sensor)
- This can easily be cleaned by blowing compressed air at this ... then cleaning the surrounding area. This is common ... not just with Mazdas and Fords ... but also Toyota Siennas.
2. The same dirty MAF sensor may need to be replaced ... they last for about 5-7 years.
3. The PCV hose might be cracked. This is a cheap part. The labor can be high though ... so call Mazda and check how much they charge. This was originally under a recall back in 2004. Mine was replaced twice ... first under a recall, secondly, when the van experienced rough idling 4 years later.
4. Oxygen sensors may need to be replaced.
Again - have the codes read ... this is an expensive part as there are two sensors... and one of them is hard to reach.
Your biggest headache is the mechanic. Many mechanics will guess the problem ... and by trial and error ---- find the root cause. A good mechanic will research the root cause and even consult with Mazda (and their own forums).