Does dirt compact itself over time? If so, how does this happen?
Soil is a collection of various sized minerals grains, of various types of minerals produced by the weathering of rock. Typical soil minerals are clays, silts and sands.

The properties and behavior of different soil types depends of the composition of the soil: the proportion of clays, silts and sand in a soil. Sandy soils are well draining and clayey soils are sticky.

Between the grains of minerals that comprise a soil are spaces, called pores or pore spaces. The pores can be filled with either water or air, depending the location of water tables and wetting events like rain, snow melts or other forms of water inundation.

The density of a soil is dependent on the degree of compaction of the soil. For to a soil to be compacted, a stress has to be applied to the soil to realign the grains of soil which reduces the total volume of the pores and reduces the amount of air within the pores.

Consolidation of a soil occurs when pore space is reduced and water in a soil is displaced due to an applied stress.

Regarding having something buried and soil compacting around it over time, yes that will occur but it is a question of how much stress the soil experiences, the duration of time and the nature of the soil - sandy or clayey. Something buried for a day without any stresses not much will happen. But, something buried for thousands of years with people and animals walking over it, rain falling on the soil, vibrations from nearby human activity and an occasional earthquake all add to the stresses the soil will experience and increases the degree of compaction or consolidation over time.