Marx did not study directly with Hegel, but after Hegel died Marx studied under one of Hegel's pupils, Bruno Bauer, a leader of the circle of Young Hegelians to whom Marx attached himself. However, Marx and Engels came to disagree with Bruno Bauer and the rest of the Young Hegelians about socialism and also about the usage of Hegel's dialectic. Having achieved his thesis on the Difference of natural philosophy between Democritus and Epicurus in 1841, the young Marx progressively broke away with the Prussian university and its teachings impregnated by German Idealism (Kant, Fichte, Schelling and Hegel).
Marx
Hegel