The origin of the enhanced hardness at the weld and HAZ in steels is undesired full or partial quenching in air. PWHT is aimed to immediate martensite decomposition into with structures with significantly lower hardeness, kine bainite, troostite, pearlite mixtures, etc. Ideally, the post treatment weld structure should be like a base metal one. PWDT often is necessary to be done immediately in order to prevent post-welding thermal cracks. The issue of dislocations density is almost irrelevant to the undesired hardness, because its origin is not a plastic deformation.
The question is so broad that it may take several hours to cover different scenario!
The previous answer by A. K.-D focused on steel welding - probably relevant to low carbon steel. The hardness profiles vary widely depending on the material composition, its initial heat treatment condition, welding process, heat input, weld constraint etc. PWHT is given not just to 'improve' the hardness profile but also to relieve the residual stress, diffuse out the hydrogen (if any), and increase the (fracture) toughness.
Note that heat treatable nickel alloys may be subjected to cracking during PWHT!
PWHT can be of different types: aging or stress relieving treatments (at relatively low temperatures < 0.5TM) or high temperature solution annealing treatment followed by aging. If the weldment is solution annealed at high temperature then all the secondary phases are dissolved which leads to a flat hardness profile. This treatment is applicable to austenitic stainless steels, nickel base alloys and age hardenable aluminum alloys. The solution treated material can be strengthened by additional aging treatments.
Dislocation density plays a role in increasing the strength of weld metal in as-welded condition. The weld metal will have a relatively high concentration of point defects and dislocations due to faster cooling and thermal stresses developed by constraints during weld pool solidification. During low temperature PWHT, the dislocations could act as nucleation sites for secondary phase precipitation.
This is very generic. The discussion on PWHT steel needs specific details.
Magnesium comes in different alloys, with different Magnesium proportion, so some alloys can be strengthened by heat treatment and some can not. Till nowdays (as I have been informed), magnesium alloys have not usually been welded; except for some repaired structures - because of the occurrence of defects - oxide films, cracks, and cavities. Another problem is high zinc content. Magnesium alloys that have a zinc content of 5-6% are sensitive to cracking and do not weld nearly as good as the ones that have about 10% aluminum content, along with lower zinc content. However, the broader application of magnesium alloys requires reliable welding processes. Repairs are mostly done using TIG, although welding magnesium components may be done by using mechanical clasps as well as with variation of welding methods including tungsten arc inert gas (TIG), plasma arc welding, electron beam welding (EBW), laser beam welding (LBW), friction stir welding (FSW), explosion, electromagnetic welding, ultrasonic welding, and resistance spot welding (RSW).