The correct option is B The shepherd sang his sheep a song.
Some verbs take two objects, a direct object and an indirect object. The indirect object is the recipient of the direct object, or an otherwise affected participant in the event. The direct object answers the question 'what?' and the indirect object answers the question 'for whom?'
In
the given sentence, "to his sheep" is the indirect object and "a song" is the
direct object. Hence option A is incorrect. (When the indirect object
is placed after the direct object, it is preceded by the preposition
'to' or 'for'.)
Option B is correct because the indirect object here is placed
before the direct.
Option C is incorrect because the sentence here means that the sheep sang a song, which is impossible.
Similarly, option D is incorrect as the sentence means that the sheep sang a song (sheep don't sing).