From the Super Mario Wiki
The Counterattack is a type of attack present in the Mario & Luigi series. When some enemies attack, it can be countered by jumping on or hammering the enemy. Almost every enemy provides a hint of which character it is going to attack (and thus which one needs to perform the counter). The counterattack's power is the same as a failed attack on the player's turn.
Mario and Luigi counterattack using their Hammers and their jump ability; in Mario & Luigi: Bowser's Inside Story, Bowser's counterattack moves are punching and ducking in his shell. If the player has someone's attack powered up, it will also boost their counterattack's power.
Counterattacks also appear in giant battles in Mario & Luigi: Bowser's Inside Story and Mario & Luigi: Dream Team. Giant Bowser or Giant Luigi use the touch screen to use their respective abilities to counterattack.