You don't need to - "Is it worth being loyal to your friends" sounds better to me.
I suspect, without evidence, that "To be worth it" is part of the old saying, "to not be worth a candle" (said of a game of cards when the cost of lighting the candle, by whose light the cards could be seen, was higher than the entertainment value of the game, and used figuratively thereafter.)
The "it" therefore is a candle.
In the alternative, the "it" is a dummy subject, as in "it is raining."
in an OED example: "2001 Independent (Nexis) 15 Sept. (Features section) 10 Vegetatively propagated stock will be more expensive but it is worth it."
It is worth it = the production of Vegetatively propagated stock is worth the expense.
It isn't doing anything there, but 'worth' requires some kind of object, which may be a gerund-participial phrase: something is worth the money, isn't worth the time, isn't worth the effort, is worth doing, is worth looking at - and if we don't mention these objects, we use dummy 'it': not worth it.
Note that in the original sentence, the first 'it' is also a dummy, but it corresponds to the infinitive clause:
It is worth it to be loyal to your friends. = To be loyal to your friends is worth it.