The win followed a narrow 27-25 victory over Sweden in their opener and the Danes were struggling again to impose themselves against modest Bahrain, leading by just 24-23 with a quarter of an hour to go.
Adding to their problems, the Danes lost star player Mikkel Hansen (two goals off three shots) with a red card after 40 minutes while injured Lasse Anderson was also sidelined.
"It was a tough game against an awkward opponent. They have a lot of qualities and it was really hard for us," admitted Denmark right winger Hans Lindberg. "They played with 'an offensive defence', it's hard to find the right solution. In attack, they have a lot of qualities in their shots."
Also in Group D, African champions Egypt defeated Argentina 31-26 to make sure of a place in the last 16.
In Group C, European champions Germany clinched a fourth win from four matches with a 31-25 win over Belarus and will next face Croatia in what will likely be a battle for top spot in the table.
Uwe Gensheimer scored eight goals and has 29 for the tournament.
"In the first half we were not good enough and always were one step behind," said Germany coach Dagur Sigurdsson.
"We lacked aggressiveness in defence, therefore it was a difficult game. After our good start in the second half, when we were ahead by three, four goals, Belarus needed to take a risk. This became our advantage."