diff --git a/src/comment.rs b/src/comment.rs
index b7d7a396a67..709031dda44 100644
--- a/src/comment.rs
+++ b/src/comment.rs
@@ -885,6 +885,9 @@ impl<'a> CommentRewrite<'a> {
                 // Remove space if this is an empty comment or a doc comment.
                 self.result.pop();
             }
+            if self.code_block_attr.is_some() && self.is_prev_line_multi_line {
+                self.result.push_str(&self.comment_line_separator);
+            }
             self.result.push_str(line);
             self.fmt.shape = Shape::legacy(self.max_width, self.fmt_indent);
             self.is_prev_line_multi_line = false;
diff --git a/tests/target/issue-5244/unwrapped.rs b/tests/target/issue-5244/unwrapped.rs
new file mode 100644
index 00000000000..79530cab82d
--- /dev/null
+++ b/tests/target/issue-5244/unwrapped.rs
@@ -0,0 +1,7 @@
+// rustfmt-wrap_comments: false
+
+/// Here is me writing some documentation that is too long oh me oh my now some code please!
+/// ```
+/// test
+/// ```
+fn foo() {}
diff --git a/tests/target/issue-5244/wrapped.rs b/tests/target/issue-5244/wrapped.rs
new file mode 100644
index 00000000000..f967b3c1c8c
--- /dev/null
+++ b/tests/target/issue-5244/wrapped.rs
@@ -0,0 +1,8 @@
+// rustfmt-wrap_comments: true
+
+/// Here is me writing some documentation that is too long oh me oh my now some
+/// code please!
+/// ```
+/// test
+/// ```
+fn foo() {}