diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index d7091593b497b1dc36cbb06ddc8997397c0ff42e..0ab12e792838bf03da1d0f3e07bc9e3c7607230e 100644
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -2588,6 +2588,18 @@ void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, stru
  */
 void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b);
 
+/*!
+ * \brief Swap endpoint_forward and endpoint_cache_forward between two channels
+ * \param a First channel
+ * \param b Second channel
+ * \return void
+ *
+ * \note
+ * This is used in masquerade to exchange endpoint details if one of the two or both
+ * the channels were created with endpoint
+ */
+void ast_channel_internal_swap_endpoint_forward_and_endpoint_cache_forward(struct ast_channel *a, struct ast_channel *b);
+
 /*!
  * \brief Set uniqueid and linkedid string value only (not time)
  * \param chan The channel to set the uniqueid to
diff --git a/main/channel.c b/main/channel.c
index c1eecdb0191f36af81b431147dda0f37d4190f0f..74d7986454a72ee19157987ed1bf40d139aa407c 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -6922,6 +6922,12 @@ static void channel_do_masquerade(struct ast_channel *original, struct ast_chann
 	/* Make sure the Stasis topic on the channel is updated appropriately */
 	ast_channel_internal_swap_topics(clonechan, original);
 
+	/* Swap endpoint forward and endpoint cache forward details of the channels,
+	 * so channel created with endpoint exchanges its state with other channel
+	 * for proper endpoint cleanup.
+	 */
+	ast_channel_internal_swap_endpoint_forward_and_endpoint_cache_forward(clonechan, original);
+
 	/* Swap channel names. This uses ast_channel_name_set directly, so we
 	 * don't get any spurious rename events.
 	 */
diff --git a/main/channel_internal_api.c b/main/channel_internal_api.c
index 72ca1a9ce231907b9d0f075fc9c70948d108fa09..b5fd87dc487c685f8304b4e90952c2d0ab363209 100644
--- a/main/channel_internal_api.c
+++ b/main/channel_internal_api.c
@@ -1546,6 +1546,18 @@ void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel
 	b->topics = temp;
 }
 
+void ast_channel_internal_swap_endpoint_forward_and_endpoint_cache_forward(struct ast_channel *a, struct ast_channel *b)
+{
+	struct stasis_forward *temp;
+	temp = a->endpoint_forward;
+	a->endpoint_forward = b->endpoint_forward;
+	b->endpoint_forward = temp;
+
+	temp = a->endpoint_cache_forward;
+	a->endpoint_cache_forward = b->endpoint_cache_forward;
+	b->endpoint_cache_forward = temp;
+}
+
 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
 {
 	ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));