diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index a6435d19fe138030d2952ae82fc88d602b567d50..59016cab9fed1a980f1fc3d15d3ca449b3853db5 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -708,6 +708,7 @@ static int mkv_write_header(AVFormatContext *s)
     if (mkv->cues == NULL)
         return AVERROR(ENOMEM);
 
+    put_flush_packet(pb);
     return 0;
 }
 
@@ -888,6 +889,7 @@ static int mkv_write_trailer(AVFormatContext *s)
 
     end_ebml_master(pb, mkv->segment);
     av_free(mkv->md5_ctx);
+    put_flush_packet(pb);
     return 0;
 }