It's not that simple.
Let's tackle the diesel first because it's easier to follow.
Understand that the diesel engine is always lean of stoichiometric (we are assuming proper calibration here, and not a dense-black-smoker). The only question is by how much.
When the engine is idling or at light load, there is a huge amount of excess air for not much fuel. So the heat of combustion (the portion that isn't used for operating the pistons!) is diluted over a great amount of mass (the excess air). At maximum load, there is not much excess air, so there is more heat relative to the amount of air, so it's hotter.
Now ... Spark ignition engine.
These are, for the most part, always stoichiometric or nearly so. With conventional engines, if they are not stoichimetric, they are rich of stoichiometric. And with that ... the situation is simply the upside-down version of what was just described. Too rich, combustion becomes incomplete, less energy is released, but there is more mass flow due to the extra fuel, so there is less heat spread over more mass ... it's cooler (but dirty).
If you can coax a spark-ignition engine to operate on the lean side, the situation is just as it is with the diesel and for the same reasons.
Normally with spark-ignition engines, the exhaust gas temperature is at a peak very close to stoichiometric (because that's where the "heat release per amount of total mass flow" is greatest) and it drops off on either side.
In reality the peak EGT is not exactly at stoichiometric because of various combustion-speed-related matters, but it's pretty close.
When people are tuning race engines (who are not scientists or engineers), their word "lean" should usually really be interpreted as "less rich" ... very rarely will a racing spark-ignition engine ever actually operate lean of stoichiometric. The way I've seen this stated in understandable terms is that if your goal is maximum power output, the amount of air will be constrained, and it has to run a little on the rich side to make sure you burn all the air. If your goal is maximum fuel economy, it has to run a little on the lean side to make sure you burn all the fuel.