The Afghan Taliban and the Iranian regime were once hostile enemies. However, this all changed after the US took action after the 9/11 terrorist attack and the enemies started to cooperate.
This is similar to the situation in Iraq in 2003 – Iran did not mind the US invasion, but it proceeded to drain its resources once the US succeeded in toppling Saddam Hussein’s regime. This is when Iran effectively took control. Iraq was handed to Iran on a plate.