This is possible with the ScriptCards Mod. It may be possible other ways as well but I'll show a quick example of one way ScriptCards could do this.
!scriptcard {{
--&DieSize|d6
--#title|Matching Dice Example
--=OriginalRoll|2[&DieSize]
--=NewTotal|[$OriginalRoll]
--~|array;define;RollArray;[$OriginalRoll.RolledDice(1)];[$OriginalRoll.RolledDice(2)]
--?[$OriginalRoll.RolledDice(1)] -eq [$OriginalRoll.RolledDice(2)]|>RollAdditionalDie;OriginalRoll;NewTotal
--~RollList|array;stringify;RollArray;,
--+Rolls|[&RollList]
--+Total|[$NewTotal]
--X|
--:RollAdditionalDie|FirstRoll;ResultingRoll
--=AddedRoll|1[&DieSize]
--~|array;add;RollArray;[$AddedRoll.Raw]
--=[%2%]|[$[%1%]] + [$AddedRoll]
--?[$AddedRoll] -eq [$OriginalRoll.RolledDice(1)]|>RollAdditionalDie;[%2%];[%2%]
--<|
}}

In that test run, the original 2d6 roll was 2 5's and then rolled another die which was a 5 and then rolled again for a 4.
I suspect there may be other ways to accomplish what you want that might even be better fits but I thought I would add a ScriptCard option for you.
Let me know if you have any questions about the ScriptCard here and I'll do my best to explain them.