Their nuances are different, and you need to decide each time which is appropriate.

When を is used:

秘書を伴って: You are the main person, and your secretary is accompanying you.

危険を伴う手術: The main event is surgery, and some danger comes with the surgery.

When に is used:

父に伴って: Dad is the main person, and you are accompanying him.

人口増加に伴う住宅問題: The main (first) event is population growth, then housing problem comes after that.