If "Yes", under what conditions can this happen?
Conjure Elemental is a D&D 5e spell available to Wizards and Druids and can be found on p. 225 of the PHB.
The spell description states:
The elemental is friendly to you and your companions for the Duration.
The spell's duration is "Concentration, Up to 1 hour". The spell ends if concentration is broken or ended, or if the Elemental reaches 0 HP.
The spell description has two scenarios by which a conjured elemental might take hostile actions against the caster and their companions, one which requires the caster's concentration being broken:
If you don’t issue any commands to the elemental, it defends itself from hostile creatures but otherwise takes no actions. If your concentration is broken, the elemental doesn’t disappear. Instead, you lose control of the elemental, it becomes hostile toward you and your companions, and it might attack.
If the Elemental is issued no commands and is attacked by a companion of the caster RAW seems to suggest that the Elemental could attack the companion which attacked it. If this is the case, would the attack that triggered this have to be intentional, or could it also be triggered by an unintentional attack? RAW also seems to suggest that upon receiving a command from the caster the elemental would cease its attack on the companion in question.
So, here's the story. First off, I want to be clear that I understand what the DM says goes & they are the final word on any rules in the game. That said, my DM has stated that they are trying to run the game as close to RAI as possible. It was also the first time my wizard Mulland successfully cast the spell Conjure Elemental, so it was a new thing, and when we started playing about 15 months ago we were all relatively new to 5e.
Mulland cast Conjure Elemental about a minute before going into combat. We were fighting about half a dozen Minotaurs in a relatively small room.
I was not RPing the 'verbal commands' requirement of the spell, to be honest, I think both the DM and I forgot about that aspect, but I had control of the creature's token and was moving/attacking/rolling for the elemental.
Cael, the cleric, attempted to cast Sacred Flame on one of the Minotaurs. Due to setting conditions in our game magic is presently unreliable. Whenever a spell is cast the caster must roll a d100 and add a modifier. The result of the roll determines the result of the spell as per a table of various effects. Cael's roll resulted in "spell affects random target in range" and the Sacred Flame ended up descending on the elemental, which passed their Dex save easily, taking no damage.
Next turn I was told the elemental was no longer under my control. I asked if my concentration had been broken. I was told it had not been broken. I said in that case I would keep concentrating. I RP'd Mulland issuing a command to the elemental to resume its attacks against the Minotaurs. The DM had me roll an Int check, which Mulland failed. Next turn I again issued a command to the elemental. I was asked for another Int check, which Mulland passed, and was told the elemental was once again under my command.
So, would this work under RAW/RAI? I think that the elemental turning against the caster and/or their companions outside of the two conditions mentioned above would be contrary to RAW/RAI, but I'd love to hear the community's thoughts.