The two construction companies pleaded guilty to breaching health and safety laws at Hull Crown Court.
The court heard that the piling rig fell over and rolled across the road before crushing a wall opposite during the evening rush hour on 10 December 2007. Tower Street was closed for several hours until it was made safe.
An HSE investigation found that the main contractor, Multibuild, was responsible for providing a stone platform for the machine to work from, but had failed to design or install it correctly.
Balfour Beatty Ground Engineering, as a specialist sub-contractor, had the responsibility for safely carrying out the work.
HSE inspector Dave Redman said: "This incident could easily have resulted in disaster, and it is nothing short of a miracle that no one was killed or seriously injured given it occurred during the peak of the evening rush hour.
"It is every company's responsibility to ensure that employees and members of the public are not exposed to danger from heavy construction machinery.
"There is extensive guidance governing safe working in this sector, and we hope today's prosecution serves to remind people of their duties so that we don't witness an incident of this kind again."
Balfour Beatty was fined £25,000 and ordered to pay £17,676 in costs. Multibuild was fined £20,000 and ordered to pay £18,687 costs.