Pinging or knocking is caused when there are two separate "explosions" in one cylinder in an engine.
In normal operation of gasoline engines the fuel/air mix is compressed by the piston then ignited by the spark plug, the one "explosion" pushes the piston down which adds power to the crankshaft.
If the fuel/air ignites a few milliseconds before the spark plug fires, you have two explosions in one cylinder, when the "wave fronts" meet they make the pinging or knocking noise you hear.
There are three things that cause pinging.
Octane level of fuel
Heat in cylinder
spark timing
Octane rating has nothing to do with power, i.e. a gallon of 87 octane and a gallon of 95 octane provide the same "power" in an engine.
You do not get better mileage per gallon with higher octane.
Octane is a compressibility number, the lower the octane number the less compression it can take before spontaneous ignition.
Higher octane fuel cost more to make so costs more at the pump.
Higher performance engines have higher compression in the cylinders, so need to use a higher octane fuel to prevent pinging.
Most cars and trucks are now designed with lower compression engines so they can use "regular" fuel without pinging.
Heat in a cylinder can pre-ignite the fuel/air, usually this happened in older engines that had unburned carbon buildup inside the cylinders, the carbon would heat up and act as a "glow plug", like those used in diesel engines, these hot spots would pre-ignite the fuel and cause pinging, and even prevent the engine from shutting off normally, they would continue to "deisel" even after key/spark was off.
The 1999 Mazda b2500 should run on "regular" gas without pinging, unless you have swapped the engine out for a built 454 V8 with 15:1 compression, lol.
Unless the engine is always running hot, I would think the timing is off, or not being changed correctly by the engine control computer.
Knock sensors were added to engines in the 1990s, so yours might have one, the sensor would tell the computer to retard the timing if knocking is detected.