With `awk`:
awk '
$0 == "foo" {if (sep) print ""; sep = ""; inside = 1; next}
$0 == "bar" {inside = 0; next}
inside {printf "%s", sep $0; sep = " "}
END {if (sep) print ""}'
To match on lines with `foo` as the first word, replace `$0 == "foo"` with `$1 == "foo"`; to match on lines starting with `foo`, replace with `/^foo/` (short for `$0 ~ /^foo/`).