Why does diesel have a higher energy density than gasoline?
Diesel and gasoline have roughly the same energy per unit mass (lower heating value), about $41~\pu{MJ/kg}$).

The density of diesel is about $833~\pu{kg/m^3}$ compared to $740~\pu{kg/m^3}$ for gasoline. This gives diesel about 13% higher energy density per volume.