Why does density decrease across the period 4 and 5 transition metals? I know that density decreases across period 4 because the last two elements are liquids and gases which means that their densities are less but why are the densities increasing and then decreasing within the transition metals themselves?
Density is generally proportional to how far from a full octet (or in your case, an 18 electron set of outer-most orbitals) a material is, hence this effect, and its inverse - that earlier elements in a period are less dense. This does not apply only to transition metals, but all elements, as illustrated here. (Note: I am not sure what pressure is in that chart, so I suggest ignoring gas-phase elements, including O, F, and Cl.)

The reason for this is fairly simple. The further from a complete octet an element is, the more energetically favorable orbital overlap becomes. The more favorable orbital overlap becomes, the closer elements will pack at a given temperature. There is no energetic benefit to an element with a full octet (ideal gases) overlapping, so they typically do not, and thus form a gas. There is an intense energy benefit in elements with both half-full octets overlapping.