Relax mate, you didn't cause any damage.
All cars, even really old ones, have got rev limiters and you would know if you'd hit it. The engine basically stalls then fires up then stalls over and over several times a second — lots of loud noises and the car might even jerk around a bit.
"Semi-automatic" cars like the one you're driving usually never hit the rev limiter, since they also have a computer in the gearbox that automatically shifts up to the next gear. Even when you're in "manual" mode it will usually shift if necessary to avoid engine excessively high or low revs.
Even if you did hit the rev limiter you really can't do any damage, especially with any car made in the last 15 years or so. Worst you can do is waste a bit of fuel.
Hitting the rev limiter several times a day is not advisable - It will cause some parts to wear out slightly faster than they otherwise would. But once or twice? Even once a month? It's not going to cause any appreciable wear on the engine.
Also: 40km/h is pretty slow. You should be able to travel almost twice that speed without hitting either the rev limiter or the automatic upshift (every car is different however - the only Mazda 3 I've driven was a manual).
I recommend you go out and experiment a bit! Find an empty country road, stick it in manual mode, and mash your foot to the floor all the way up to the speed limit. If it shifts up for you, then you know you don't have to worry about it. If it doesn't shift up and you hit the rev limiter... well, hitting it once in a blue moon doesn't hurt anything. Racing car drivers hit the rev limiter several times every minute, and they're driving highly tuned fragile engines - not the rock solid reliable engine in a Mazda 3.
A few things to watch out for... pushing the engine hard should be done when the engine is warm, not cold. So make sure it's been running for 3 or 4 minutes first. Keep an eye on the engine temperature and for any warning lights on the dashboard. Finally, don't let it stay on the rev limiter for any longer than you need to. A couple seconds is fine, several seconds is bad, not hitting it at all is ideal — but you cannot avoid hitting it unless you know where it is!).
Personally, I don't like driving a car unless I know what it's doing. And that includes knowing what will happen if I bump the lever into the wrong position. Surprises in traffic are highly distracting, and that leads to accidents. Safety first, money second.