Melee counter-attack is an easy way to make ranged attacks valuable.
Do you build and use units that have strong melee attacks but die more easily because they're in melee or do you use units with weaker ranged attacks that live longer because they can stay out of melee?
Melee counter-attack reduces otherwise over-whelming attacker advantage.
In a game like HoMM, having a large stack of powerful attackers you'll be able to kill many other stacks in the game with a single attack. Melee counter-attack means that even defenders that you roll over still whittle away at your offensive stack, reducing its offensive power over time (unless you expend resources replenishing the stack).
Melee counter-attack reduces the benefit of taking the first turn.
Continued from above, melee-counterattack means that it matters less which player attacks first, as both players' units will get a chance to attack no matter the outcome. The first attacker will no longer be able to eliminate the opponent's strongest attacker in the first turn without consequences (well, except for ranged attackers, but that plays back into the first point). This reduces the first-turn advantage to mostly being about picking the battlefield positioning, which is more strategically interesting in a tactical game.
Melee counter-attack is "easy".
This is a two-parter that is predicated on accepting the prior three points. Basically, assuming you want the additional strategic impact provided by melee-counterattack in the first place, it's just easier to use melee-counterattack than to come up with a different solution.
Melee counter-attack is widely understood and easy to incorporate into a game's design. It's easy to teach to new players which makes the game more appealing to a wider playerbase. It requires little additional/special artwork or animations. It requires little additional code.
In short, if you accept the previous three points, using melee-counterattack to achieve them is just probably far easier overall than using something else.
Melee-counterattack is a composable design.
What I mean by "composable" is that since counter-attack is such a simple rule, there are many ways to combine it with other rules to get a combinatoric growth in strategic depth without requiring much extra work. For instance, make a defender unit that does 2x damage on a counter-attack, or make an offensive unit that does 1/2x damage on a counter-attack. You'll frequently find this element in simpler designs: you can combine simple rules far more effectively than you can combine complex rules, and the result is often equally simple and easy to understand.
Melee-counterattack provides some counter-balance to unit counts.
This one is specific to other game details. An example again would be HoMM: a single unit on the battlefield is actually a stack of creatures rather than a single creature. Bigger stacks have more power. Without counter-attack, it becomes much easier for multiple small stacks to overwhelm a single large stack, even though the large stack possibly represents more overall individual creatures. The counter-attack evens out the economy of attack actions with the smaller stacks; 5 small stacks ganging up on a larger stack do not get 5x as many attacks, possibly nulling out the supposed numeric superiority of the larger stack.
This is rather subjective as its quite arguable whether you'd want to encourage the player to have many smaller stacks or fewer larger stacks.