Engine revving

2010 CX-9 GT
Sounds like a throttle body issue to me. High idle and revving can be caused by the throttle position sensor or the idle air control system. I'm not well versed in the implementation details of the Mazda throttle body but I'd start there.

Did you have the throttle body cleaned or anything like that done recently?
2017 Mazda 6 Sport
The same thing used to happen with my old 2012 Mazda 6. Sometimes when I was coming to a stop, the engine would surge and the car would lurch forward.