When a firearm is discharged, there are multiple forces acting upon it. The gases generated by the burning propellant of the cartridge, pushes the bullet out the barrel and also pushes back on the firearm. This force acts on the centerline axis of the barrel. Let this force be represented by A in the two diagrams above. Now, the user of the firearm is also resisting the kick, by holding the firearm with his hands. Let these forces be represented by B in the diagrams above. Note that for the assault rifle, there are two points where force B acts upon (i.e.) at the stock and at the grip. Also note that in both diagrams, force A is acting at a higher point than force B. This causes the rotational torque C, which causes the front of the firearm to rise.
This effect is more visible for heavier caliber weapons or when multiple shots are fired rapidly. There must be some way to prevent this muzzle climb for happening, because no one wants their weapon to act as though it is on anti-aircraft duty, when they're trying to shoot at a target on the ground!
One way to reduce this effect is if A and B are in the same axis (or fairly close to the same height), then the torque C is minimized. Another way to reduce this is to use devices such as a compensator or a muzzle brake.
In the above picture, we observe that the gentleman in the front of the picture is firing an AKM assault rifle. Notice that the tip of the barrel has a slanted tip and is slightly tilted to the right as well (it is more visible in the enlarged image). This slanted tip is not due to wear and tear or poor maintenance of the weapon -- it is actually a feature. That device is called a slant compensator. What it does is direct some of the escaping gases upwards, thereby pushing the muzzle down. This counteracts the torque trying to push the muzzle up and thereby makes it easier to keep the weapon pointed at the target. The AKM also has a tendency to move to the right, which is why the compensator is also slightly tilted that way, so that it pushes the muzzle to the left to counteract that movement as well. When the Russians decided to make an improved AK-47 version (the AKM), this was one of the standard features added to the AKM model. This simple device could also be retrofitted to older AK-47 models, simply by screwing on this device to the tip of the barrel.
Another device that is similar is called a muzzle brake. Basically, this consists of a small tube that has a number of perforations along its sides, which is attached to the end of the muzzle. These perforations allow the gases to escape at an angle to the muzzle, which not only helps reduce the muzzle climb, but also reduces the recoil felt at the back of the weapon.
Yet another way to reduce recoil is called porting. Basically, this involves drilling precise holes near the end of the barrel, which allows some of the gases behind the bullet to escape upwards as the bullet is exiting the barrel, thereby pushing the barrel down.
The advantage of such devices is that they reduce muzzle climb as well as felt recoil. This improves the accuracy of the user. It also reduces the load felt by the user during long shooting sessions.
There are also a few disadvantages of such devices. One is that the noise level of the weapon increases by a few decibels. They also increase the flash seen at the end of the barrel. Some muzzle brakes redirect some of the gases towards the user to reduce recoil, but this could cause some of the pressure wave to be redirected to the user as well as extra noise and flash, thereby causing some discomfort. Additionally, if the shooter is firing from a prone position, the redirected gases could throw up some extra dust towards the shooter, which could impair visibility and give away the user's position. Finally, the extra weight at the tip could affect the overall balance and handling of the weapon.