Sejad Salihovic gave Hoffenheim the lead from the penalty spot in the fifth minute.
Heiko Westermann equalised in the 45th minute for Hamburg before Petric's winner in the 83rd minute.
Hoffenheim grabbed the lead after just six minutes from the spot when Salihovic blasted his plenty into the top corner after Jonathan Pitroipa had brought down Demba Ba in the area.
Hamburg nearly equalised eight minutes later but Mladen Petric's header went just wide from 11 yards out.
The home side again went close after 18 minutes but Jose Paolo Guerrero shot wide from a tight angle after Hoffenheim goalkeeper Daniel Haas was beaten.
The rest of the first half lacked real highlights until Hamburg grabbed the equaliser in the 45th minute.
Guy Demel's long free-kick from the right was volleyed at the back post by Petric back across goal to Westermann, who slotted home from two yards to make it 1-1.
Hoffenheim could not grab the lead 10 minutes after the break as Ba connected on Salihovic's free-kick but found only crossbar.
Petric nearly gave Hamburg the lead in the 73rd minute but his long distance blast went just over the crossbar.
The Croatian scored his goal 10 minutes later though after a superb cross from the left by Ze Roberto with Petric heading home the winner at the far post.